Example usage for javax.swing MenuSelectionManager getSelectedPath

List of usage examples for javax.swing MenuSelectionManager getSelectedPath

Introduction

In this page you can find the example usage for javax.swing MenuSelectionManager getSelectedPath.

Prototype

public MenuElement[] getSelectedPath() 

Source Link

Document

Returns the path to the currently selected menu item

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    MenuSelectionManager.defaultManager().addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent evt) {
            MenuSelectionManager msm = (MenuSelectionManager) evt.getSource();
            MenuElement[] path = msm.getSelectedPath();
            if (path.length == 0) {
            }/*from  w  ww.  j a  va2  s  .  c  o m*/
        }
    });
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    MenuSelectionManager.defaultManager().addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent evt) {
            MenuSelectionManager msm = (MenuSelectionManager) evt.getSource();
            MenuElement[] path = msm.getSelectedPath();
            if (path.length == 0) {
            }/*from w w  w.ja  va 2 s. co m*/
        }
    });

}

From source file:ToggleSample.java

private MenuElement[] getPath() {
    MenuSelectionManager menuSelectionManager = MenuSelectionManager.defaultManager();
    MenuElement oldPath[] = menuSelectionManager.getSelectedPath();
    MenuElement newPath[];/*  ww w .j  a v a 2s  .  co m*/
    int oldPathLength = oldPath.length;
    if (oldPathLength == 0)
        return new MenuElement[0];
    Component parent = getParent();
    if (oldPath[oldPathLength - 1].getComponent() == parent) {
        // Going deeper under the parent menu
        newPath = new MenuElement[oldPathLength + 1];
        System.arraycopy(oldPath, 0, newPath, 0, oldPathLength);
        newPath[oldPathLength] = this;
    } else {
        // Sibling/child menu item currently selected
        int newPathPosition;
        for (newPathPosition = oldPath.length - 1; newPathPosition >= 0; newPathPosition--) {
            if (oldPath[newPathPosition].getComponent() == parent) {
                break;
            }
        }
        newPath = new MenuElement[newPathPosition + 2];
        System.arraycopy(oldPath, 0, newPath, 0, newPathPosition + 1);
        newPath[newPathPosition + 1] = this;
    }
    return newPath;
}

From source file:JToggleButtonMenuItem.java

private void init() {
    updateUI();//from   w  w  w .j ava 2  s  . c  om
    setRequestFocusEnabled(false);
    // Borrows heavily from BasicMenuUI
    MouseInputListener mouseInputListener = new MouseInputListener() {
        // If mouse released over this menu item, activate it
        public void mouseReleased(MouseEvent mouseEvent) {
            MenuSelectionManager menuSelectionManager = MenuSelectionManager.defaultManager();
            Point point = mouseEvent.getPoint();
            if ((point.x >= 0) && (point.x < getWidth()) && (point.y >= 0) && (point.y < getHeight())) {
                menuSelectionManager.clearSelectedPath();
                // Component automatically handles "selection" at this point
                // doClick(0); // not necessary
            } else {
                menuSelectionManager.processMouseEvent(mouseEvent);
            }
        }

        // If mouse moves over menu item, add to selection path, so it becomes
        // armed
        public void mouseEntered(MouseEvent mouseEvent) {
            MenuSelectionManager menuSelectionManager = MenuSelectionManager.defaultManager();
            menuSelectionManager.setSelectedPath(getPath());
        }

        // When mouse moves away from menu item, disarm it and select something
        // else
        public void mouseExited(MouseEvent mouseEvent) {
            MenuSelectionManager menuSelectionManager = MenuSelectionManager.defaultManager();
            MenuElement path[] = menuSelectionManager.getSelectedPath();
            if (path.length > 1) {
                MenuElement newPath[] = new MenuElement[path.length - 1];
                for (int i = 0, c = path.length - 1; i < c; i++) {
                    newPath[i] = path[i];
                }
                menuSelectionManager.setSelectedPath(newPath);
            }
        }

        // Pass along drag events
        public void mouseDragged(MouseEvent mouseEvent) {
            MenuSelectionManager.defaultManager().processMouseEvent(mouseEvent);
        }

        public void mouseClicked(MouseEvent mouseEvent) {
        }

        public void mousePressed(MouseEvent mouseEvent) {
        }

        public void mouseMoved(MouseEvent mouseEvent) {
        }
    };
    addMouseListener(mouseInputListener);
    addMouseMotionListener(mouseInputListener);
}

From source file:ToggleSample.java

private void init() {
    updateUI();/* w w w . j  a v  a  2 s .  c  om*/
    setRequestFocusEnabled(false);
    // Borrows heavily from BasicMenuUI
    MouseInputListener mouseInputListener = new MouseInputListener() {
        // If mouse released over this menu item, activate it
        public void mouseReleased(MouseEvent mouseEvent) {
            MenuSelectionManager menuSelectionManager = MenuSelectionManager.defaultManager();
            Point point = mouseEvent.getPoint();
            if ((point.x >= 0) && (point.x < getWidth()) && (point.y >= 0) && (point.y < getHeight())) {
                menuSelectionManager.clearSelectedPath();
                // component automatically handles "selection" at this point
                // doClick(0); // not necessary
            } else {
                menuSelectionManager.processMouseEvent(mouseEvent);
            }
        }

        // If mouse moves over menu item, add to selection path, so it
        // becomes armed
        public void mouseEntered(MouseEvent mouseEvent) {
            MenuSelectionManager menuSelectionManager = MenuSelectionManager.defaultManager();
            menuSelectionManager.setSelectedPath(getPath());
        }

        // When mouse moves away from menu item, disaarm it and select
        // something else
        public void mouseExited(MouseEvent mouseEvent) {
            MenuSelectionManager menuSelectionManager = MenuSelectionManager.defaultManager();
            MenuElement path[] = menuSelectionManager.getSelectedPath();
            if (path.length > 1) {
                MenuElement newPath[] = new MenuElement[path.length - 1];
                for (int i = 0, c = path.length - 1; i < c; i++) {
                    newPath[i] = path[i];
                }
                menuSelectionManager.setSelectedPath(newPath);
            }
        }

        // Pass along drag events
        public void mouseDragged(MouseEvent mouseEvent) {
            MenuSelectionManager.defaultManager().processMouseEvent(mouseEvent);
        }

        public void mouseClicked(MouseEvent mouseEvent) {
        }

        public void mousePressed(MouseEvent mouseEvent) {
        }

        public void mouseMoved(MouseEvent mouseEvent) {
        }
    };
    addMouseListener(mouseInputListener);
    addMouseMotionListener(mouseInputListener);
}