Results 1 to 4 of 4
Thread: Using a JMenu as a real option
- 11-09-2009, 01:08 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 7
- Rep Power
- 0
Using a JMenu as a real option
Hi,
I'm setting up a menu where I have an "exit" menu on the right of the JMenuBar.
I already tried to use a JMenuItem instead of a JMenu to do the job, but the L&F of the menu is different from a JMenu (the vertical gradient isn't draw) and I can't control its width.
If I use a JMenu without any JMenuItems associated, the menu is selected when I click it and doesn't work properly.
The most aproximate result that I was able to come up was using a JMenu for the purpose and on the selected event I change the "selected" property of the menu (with the setSelected method) to false. Unfortunately, if the user cancels the Exit option (on a confirmation dialog that pops up) I can see the "remains" of the JPopupMenu (which is empty) and if I click again on the menu, aside from the fact that the "selected" event isn't fired, the menu is selected.
So, how would you solve this issue? How can I make an EXIT menu which allows the user to terminate the application by clicking an option on the first level of a JMenuBar?
Best regards
- 11-09-2009, 01:37 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
I'd do it the way all other applications do it. That is make "Exit" a menu item of the first menu and then add an accelerator. That way you don't have to code around problems you find when you use components in a non standard way.
But if you really want to travel the road you are on then you can try wrapping your "exit code" in a SwingUtilities.invokeLater(). This will hopefully allow the menu to be repainted in its normal state before the confirmation dialog appears.
Or maybe you use a JButton, without Borders.
- 11-09-2009, 01:43 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 7
- Rep Power
- 0
This is more of a theory exercise than anything else. The whole idea is to implement a menu that responds imediately instead of showing a list of menu items.
As for the suggestion you made, I'm not experiencing any drawing issues. The "remains" of the JPopupMenu that I mentioned is normal, although I'm trying to prevent it from being shown. If you create a JMenu on a JMenuBar without any JMenuItem or other JMenu associated, you'll see a box with about 2x2 pixels where the menu options usually show.
- 11-09-2009, 05:04 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 7
- Rep Power
- 0
I think your suggestion about using a JButton is the best option. Nonetheless, I didn't find a way to disguise the component so that it seamlessly integrates the MenuBar.
The thing I'm missing is the visual feedback when the user clicks on the JButton. I already removed the border and set the contentAreaFilled property to false in order to prevent the button's background from being drawn. Because this property is set to false, the background of the button doesn't change when the user presses the button.
Is there an easy way to hide everything about a button except for its behavior when pressed?
Similar Threads
-
link between two JFrame using JMenu
By JavaEbony in forum NetBeansReplies: 8Last Post: 03-09-2011, 11:19 AM -
JMenu with Tabs using swing
By phil128 in forum AWT / SwingReplies: 1Last Post: 03-03-2009, 03:29 PM -
JMenu to JPopupMenu
By carderne in forum New To JavaReplies: 0Last Post: 02-28-2009, 06:07 PM -
JMenu calling another Forms/Panels
By plodos in forum New To JavaReplies: 2Last Post: 12-10-2007, 08:02 AM -
JMenu and JRadioButtonMenuItem
By doron70 in forum AWT / SwingReplies: 3Last Post: 07-18-2007, 06:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks