Example usage for javax.swing MenuSelectionManager setSelectedPath

List of usage examples for javax.swing MenuSelectionManager setSelectedPath

Introduction

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

Prototype

public void setSelectedPath(MenuElement[] path) 

Source Link

Document

Changes the selection in the menu hierarchy.

Usage

From source file:JToggleButtonMenuItem.java

private void init() {
    updateUI();/*  w w w.  ja  v a 2s  . c  o  m*/
    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();//ww  w  .j  av  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);
}