Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Way to share JMenuItems with JMenus and JPopupMenu?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know of a way to 'share' JMenuItems with both JMenus and JPopupMenus - for instance:
JMenuItem mniExample1 = new JMenuItem("Example 1");
JMenu mnuExample = new JMenu("Examples");
JPopupMenu pmExample = new JMenu("Examples");
mnuExample.add(mniExample1);
pmExample.add(mniExample1);
What happens when I do this is that the Example1 menuItem only shows up in the popup menu not both.
Thanks
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not share JMenu and JMenuItem instances. these are view components, and you definitely need an instance each time you want it displayed. what you can (and should) share is the listener that you attach to each menu item. or you can use the class Action (see the API)
Chantal
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the API documentation - there is a method in the JMenu class that spits out a JPopupMenu that is exactly like the JMenu.
Hope that helps.
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic