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);