5

I've inserted a JMenu (instance named: exitMenu) without any JMenuItem, so my intention is to make available a way to EXIT the program without access unnecessary menu items, since my program has just one JMenu object (Someone might says: WTF!!! but...).

Thus, to capture the event occurred in this specific JMenu component, my class implements the MenuListener interface. As everybody knows, there are three mandatory implementation methods, although I need to use just one, the menuSelected() method.

To make my program a little bit intuitive, undoubtedly, once the user selects the exitMenu, the (in)famous popup JOptionPane.showConfirmDialog() presents itself where he/she needs to choose between the YES or NO option.

If the chosen option is YES, no problem at all, since the program is finished through System.exit(0). The problem is the NO option, when the focus returns to the program, the exitMenu remains selected, off course, since I've selected previously. The "thing" I'd like to do is to remove the object selection right after the NO option is chosen, so the user'll be able to click on it again.

Even using exitMenu.setSelected(false) within the three mandatory methods (one calling another), although exitMenu component is "deselected" it's necessary to click on it twice to call its event listener.

Any suggestion?

Thanks in advance.

LucDaher
  • 157
  • 2
  • 12

2 Answers2

7

One thing I attempted is to simply call setSelected(false) from within the menuSelected(...) method, but this has side effects. For one, the menu doesn't appear to be selected, and for another, it doesn't work all the time.

One possible solution that does work is to deselect the menu in a Swing Timer. Something like:

     @Override
     public void menuSelected(MenuEvent mEvt) {
        // show JOptionPane
        // if yes selected, exit. 

        // Otherwise...
        final JMenu menu = (JMenu) mEvt.getSource();
        new Timer(200, new ActionListener() {

           @Override
           public void actionPerformed(ActionEvent e) {
              menu.setSelected(false);
              ((Timer)e.getSource()).stop();
           }
        }).start();
     }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Interesting approach... more work to do I'd have to use Timer... I'll try and I'll give a feedback. Thanks. – LucDaher Apr 17 '12 at 01:57
  • Sorry... didn't work either. But there might be a solution... They didn't make available such Event without thinking how-to work around this issue. – LucDaher Apr 17 '12 at 02:01
  • @LucDaher: It works fine for me in my test program. If it didn't work for you, then you should create a small compilable and runnable test program that shows this, and also allows us to see and experience your necessary conditions. – Hovercraft Full Of Eels Apr 17 '12 at 02:04
  • 1
    I discovery what is the problem and, perhaps, it has worked for you because you might use any Microsoft Windows OS (or some OS different than Linus with Gnome GTK+ as Window Manager). I've forgot to mention that the underlying OS where my test environment lies is Ubuntu and I'm using Gnome GTK+ as my default Look And Feel. As soon I've tested over my guest WinXP virtualbox and set the L&F as "Window", using your suggestion (or simply exitMenu.setSelected(false)) , it worked flawlessly. Note: I tried with Metal L&F on the WinXP and it's not worked as well, only with Window (or Classic) L&F. – LucDaher Apr 18 '12 at 03:28
  • @LucDaher: Thanks for the getting back to us with the problem and its solution! – Hovercraft Full Of Eels Apr 18 '12 at 04:01
2

There are two levels, for

  • JMenu there is MenuListener

  • JMenuItem there is ButtonModel

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
mKorbel
  • 109,525
  • 20
  • 134
  • 319