开发者

How to catch click on a JMenu element?

I'm developping a Java/Swing application with a menu using JMenuBar, JMenu and JMenuItem.

System look and fell is applied to the UI.

UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );

I have several top level elements for my menu :

  • The JMenu "File" works fine and allows the user to select submenus like JMenuItems "New", "Open"...

  • Then, I wanna add a second top level element "About" (same level as "File") that displays a popup when clicking on it.

The issue is quite simple: how to make this JMenu "About" element clickable?

I try several ways:

  1. Set the "About" element as a JMenuItem. However, this element has not the same UI that the other JMenu items like "File"

  2. Set the "About" element as 开发者_开发百科a JMenu and add a MouseListener and a KeyListener on it. This element will have the same UI that others but I have to click twice on the "Ok" button of the "About" popup to catch this event.

Any other solutions ?


My first suggestion would be to just add a single JMenuItem inside the JMenu "About" menu. As this would match how the other menus operate and would look the same. It would also not be surprising when a user clicks on "About" expecting a menu but sees a popup instead. It is fairly common for applications to have a "Help" menu on the end of a menu bar that has a menu item named "About" that shows a popup. It is not common for the menu title to open a popup when clicked on.

If you really have to do what your propose then either:

  • Specify a Look and Feel (like Nimbus), and modify a JMenuItem to look like a Menu Item.

or

  • Find a way for your second option to work. This sounds doable, but might take some more tweaking and would be more difficult to get the UI to work right with the System Look and Feels.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜