Example usage for javax.swing MenuSelectionManager clearSelectedPath

List of usage examples for javax.swing MenuSelectionManager clearSelectedPath

Introduction

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

Prototype

public void clearSelectedPath() 

Source Link

Document

Tell the menu selection to close and unselect all the menu components.

Usage

From source file:JToggleButtonMenuItem.java

public void processMouseEvent(MouseEvent mouseEvent, MenuElement path[], MenuSelectionManager manager) {
    // For when mouse dragged over menu and button released
    if (mouseEvent.getID() == MouseEvent.MOUSE_RELEASED) {
        manager.clearSelectedPath();
        System.out.println("Selected: JToggleButtonMenuItem, by MouseEvent");
        doClick(0); // inherited from AbstractButton
    }/*from   ww w  . jav a 2s. c om*/
}

From source file:ToggleSample.java

public void processMouseEvent(MouseEvent mouseEvent, MenuElement path[], MenuSelectionManager manager) {
    // For when mouse dragged over menu and button released
    if (mouseEvent.getID() == MouseEvent.MOUSE_RELEASED) {
        manager.clearSelectedPath();
        doClick(0); // inherited from AbstractButton
    }/*from ww  w .  java2  s  .c  o  m*/
}

From source file:JToggleButtonMenuItem.java

public void processKeyEvent(KeyEvent keyEvent, MenuElement path[], MenuSelectionManager manager) {
    // If user presses space while menu item armed, select it
    if (getModel().isArmed()) {
        int keyChar = keyEvent.getKeyChar();
        if (keyChar == KeyEvent.VK_SPACE) {
            manager.clearSelectedPath();
            System.out.println("Selected: JToggleButtonMenuItem, by KeyEvent");
            doClick(0); // inherited from AbstractButton
        }/*from w  w  w .  ja  v  a 2  s. c  om*/
    }
}

From source file:JToggleButtonMenuItem.java

private void init() {
    updateUI();/*from   w w w .  j  a  va 2  s  .  co 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

public void processKeyEvent(KeyEvent keyEvent, MenuElement path[], MenuSelectionManager manager) {
    // If user presses space while menu item armed, select it
    if (getModel().isArmed()) {
        int keyChar = keyEvent.getKeyChar();
        if (keyChar == KeyEvent.VK_SPACE) {
            manager.clearSelectedPath();
            doClick(0); // inherited from AbstractButton
        }/*  ww  w . j av  a 2 s.  c o m*/
    }
}

From source file:ToggleSample.java

private void init() {
    updateUI();/*from w w w . j  av  a 2s  . com*/
    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);
}