Example usage for javax.swing MenuSelectionManager processMouseEvent

List of usage examples for javax.swing MenuSelectionManager processMouseEvent

Introduction

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

Prototype

@SuppressWarnings("deprecation")
public void processMouseEvent(MouseEvent event) 

Source Link

Document

When a MenuElement receives an event from a MouseListener, it should never process the event directly.

Usage

From source file:JToggleButtonMenuItem.java

private void init() {
    updateUI();//  w  ww.jav  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();//from w ww  .ja  v  a2  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, 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);
}