• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sharing JMenuItems

 
Jess Ryan
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'm stumped. I've searched the 'net but found nothing... probably because no one else in the world has ever encountered this issue.

The situation is that I've got a few menu items that I want to share between an edit menu (in a jmenubar) and a popup menu. I build the popup menu first, then add the menu bar. Here's the 'issue' part: when I add the shared menu items to the edit menu, they are removed from the popup menu.. AND, only one is added to the edit menu (where, for example, there are 4 menu items that I'm adding). What in the world is happening? Below is a runnable example that just prints out the number of items in the menus to show my point.

Any insight will be hugely appreciated!

 
Philippe Nizer
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jess,

I think it is a normal behavior of swing. JMenuItems can belong to only one JMenu. I resolved my problem creating a method that always returns a new JMenuItem.

Regards,
Philippe.
 
Jess Ryan
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah. Alrighty, I'll do the same. Thanks!!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Philippe" please read the important administrative private message I just sent you.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Philippe is correct; you cannot have the same JComponent in two different places.
What you actually need is to add the same ActionListener object to both JMenuItems.
 
Rob Spoor
Sheriff
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Action objects (hint: extend AbstractAction) to do all the hard work. These Action objects can be shared among multiple user interface components; buttons, menu items, basically anything that can accept an Action.

If you then disable this single shared Action object, all components that are created with it will also get disabled.
 
Jess Ryan
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone!

Actions sound like a good solution. I just may do that when it's refactor time.

Thanks again!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic