How do I prevent a JMenuItem from closing the menu when the JMenuItem is clicked?
The JMenuItem is enabled.
So this is the scenario, I have 3 JMenuItems:
JMenuItem: A, B, C;
C displays an integer X.
A and B are used to increment or decrement X by a value of 1.
If A or B is clicked, the default nature is that the menu will close upon click.
I want to be able to repeatedly click A or B and have the menu remain up, and perform the associated ‘action’ upon each click.
Thanks!
First, using a menu to do this may be the wrong approach.
JSpinnerseems more appropriate. However, to do this with aJMenuItemyou can subclass theMenuItemUIof theLookAndFeelyou are using, and override thedoClick(...)method so thatclearSelectionPath()is not called, which closes the menu when the item is clicked.Example, if you are using the Motif
LookAndFeelyou can do this:I haven’t ever tried this myself but I think it will work.