• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sharing JMenuItems

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah. Alrighty, I'll do the same. Thanks!!
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Philippe" please read the important administrative private message I just sent you.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic