0

While making some small app in swing I have encountered a little issue. I have a JMenuBar with few JMenus, some of which have MenuListeners added and they act like normal buttons (menuSelected opens a dialog). The problem is, when I click on a 'normal' Menu and the list of JMenuItems unfolds and then move cursor over the 'buttony' Menu, the MenuListener thinks that it's clicked and lanuches appropiate method. How to disable that? Here is some part of the code cleaned of custom names:

 JMenuBar bar = new JMenuBar();

 addPlayerButton = new JMenu("Button");

 addPlayerButton.addMenuListener(new MenuListener() {

     @Override
     public void menuSelected(MenuEvent e) {
         addPlayerButton.setSelected(false);
         //here comes my method to show custom dialog
     }

     @Override
     public void menuDeselected(MenuEvent e) {
     }

     @Override
     public void menuCanceled(MenuEvent e) {
     }
 });

 bar.add(addPlayerButton);

 JMenu menu = new JMenu("Menu");
 bar.add(menu);
joval
  • 466
  • 1
  • 6
  • 15

2 Answers2

0

I think you may use an actionListener and no a MenuListener, becose menuSelected calls when the mouse is over the menuItem.I'm not sure, but try.

ferchoj
  • 91
  • 1
  • 1
  • 4
0

Maybe I have presented it in too complicated way.

Imagine you have a Frame with MenuBar and two Menus in it. You click on one of them and it shows it's content. Now you move cursor over the second menu and first closes while second opens. I simply want second not to open in such case.

joval
  • 466
  • 1
  • 6
  • 15