public void mousePressed(MouseEvent e) {
      MenuSelectionManager manager = MenuSelectionManager.defaultManager();
      JMenu menu = (JMenu) menuItem;
      manager.processMouseEvent(e);

      // Menu should be displayed when the menu is pressed only if
      // it is top-level menu
      if (menu.isTopLevelMenu()) {
        if (menu.getPopupMenu().isVisible())
          // If menu is visible and menu button was pressed..
          // then need to cancel the menu
          manager.clearSelectedPath();
        else {
          // Display the menu
          int x = 0;
          int y = menu.getHeight();

          manager.setSelectedPath(getPath());

          JMenuBar mb = (JMenuBar) menu.getParent();

          // set selectedIndex of the selectionModel of a menuBar
          mb.getSelectionModel().setSelectedIndex(mb.getComponentIndex(menu));
        }
      }
    }
    public void mouseEntered(MouseEvent e) {
      /* When mouse enters menu item, it should be considered selected

       if (i) if this menu is a submenu in some other menu
          (ii) or if this menu is in a menu bar and some other menu in a menu bar was just
               selected. (If nothing was selected, menu should be pressed before
               it will be selected)
      */
      JMenu menu = (JMenu) menuItem;
      if (!menu.isTopLevelMenu()
          || (menu.isTopLevelMenu()
              && (((JMenuBar) menu.getParent()).isSelected() && !menu.isArmed()))) {
        // set new selection and forward this event to MenuSelectionManager
        MenuSelectionManager manager = MenuSelectionManager.defaultManager();
        manager.setSelectedPath(getPath());
        manager.processMouseEvent(e);
      }
    }
 /**
  * This method is called when menu is selected. It sets selected index in the selectionModel of
  * the menu parent.
  *
  * @param e The MenuEvent.
  */
 public void menuSelected(MenuEvent e) {
   JMenu menu = (JMenu) menuItem;
   if (menu.isTopLevelMenu()) ((JMenuBar) menu.getParent()).setSelected(menu);
   else ((JPopupMenu) menu.getParent()).setSelected(menu);
 }
 /**
  * This method is called when menu is deselected. It clears selected index in the selectionModel
  * of the menu parent.
  *
  * @param e The MenuEvent.
  */
 public void menuDeselected(MenuEvent e) {
   JMenu menu = (JMenu) menuItem;
   if (menu.isTopLevelMenu()) ((JMenuBar) menu.getParent()).getSelectionModel().clearSelection();
   else ((JPopupMenu) menu.getParent()).getSelectionModel().clearSelection();
 }