Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JMenu - Selected and Deselected feature

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.

like image 465
LucDaher Avatar asked Apr 16 '12 21:04

LucDaher


People also ask

What is JMenu in Java?

JMenuBar is an implementation of menu bar . the JMenuBar contains one or more JMenu objects, when the JMenu objects are selected they display a popup showing one or more JMenuItems . JMenu basically represents a menu . It contains several JMenuItem Object . It may also contain JMenu Objects (or submenu).

Which of the following methods are used to add the JMenultem into JMenu JMenu into JMenuBar and JMenuBar into JFrame?

setJMenuBar(theJMenuBar); As the code shows, to set the menu bar for a JFrame , you use the setJMenuBar method. To add a JMenu to a JMenuBar , you use the add(JMenu) method. To add menu items and submenus to a JMenu , you use the add(JMenuItem) method.

How to add menuBar in frame in Java?

JFrame myframe = new JFrame(); JMenuBar menubar = new JMenuBar(); JMenu menu = new JMenu("size"); JMenuItem size = new JMenuItem("size"); menu. add(size); menubar. add(menu); myframe. setJMenuBar(menubar);

Which constructor is used to add menu item when menu is selected?

The JMenuBar class is used to display menubar on the window or frame. It may have several menus.


2 Answers

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();
     }
like image 77
Hovercraft Full Of Eels Avatar answered Oct 06 '22 10:10

Hovercraft Full Of Eels


There are two levels, for

  • JMenu there is MenuListener

  • JMenuItem there is ButtonModel

like image 44
mKorbel Avatar answered Oct 06 '22 09:10

mKorbel