• 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

JPopupMenu JMenuItem and JMenuItem : Listen to each other?

Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an application with a JMenu - a 'File' menu. Amongst other items, this menu has a JMenuItem that triggers saving files when clicked. This JMenuItem starts life in a disabled state (so the user cannot save anything) until something in the project is added, deleted or modified, at which point this 'Save' JMenuItem's state changes to enabled - allowing the user to save the altered project. Once this saving is completed, the 'Save' JMenuItem's state returns to disabled and so on.

The reason for this is that it simplifies the logic behind saving files that have not been altered, closing an altered file needing to trigger a check to see if the project data has altered since... blah, blah, blah. It seems simpler to me to just toggle the state of the 'Save', 'Save as', 'Close', 'Exit', etc JMenuItems appropriately when the user alters some data in a project.

All of this works perfectly when the components that the user is using to alter the data are contained within the same JFrame as the 'File' JMenuItems as the appropriate JMenuItems can be referenced directly as everything is all in the same big application gui class.

It falls down for me when I try to apply the same thing to JPopupMenu's...

Elsewhere in the program I have a JTree that has a customised JPopupMenu that extends JPopupMenu. Amongst other items, this PopupMenu has a JMenuItem that triggers adding a chapter when clicked.

All the above works fine using ActionListeners and actionPerformed method calls.

This is what I want to add but I am very confused as to how to do it.

When the user clicks on the 'Add Chapter' JMenuItem in the customised JPopupMenu, aside from adding the chapter, I want it to send a message to the 'Save' JMenuItem in the 'File' JMenu, telling the 'Save' JMenuItem to change its state to enabled.

As the 'Save' JMenuItem is not a field within the custom JPopupMenu class, I can't reference it directly. I could pass the 'Save' JMenuItem into the JPopupMenu and reference it that way but that seems to tie the two objects very tightly together. I could customise the 'Save' JMenuItem and have it implement the ActionListener interface and so on but as my overall understanding of Java's inbuilt messaging system isn't perfect I started to suspect I may be trying to re-invent Java's messaging system poorly.

I am also using Netbeans which is a very useful tool but is also still a little confusing sometimes.

How do I use Java's messaging system to make the 'Save' JMenuItem listen for and react to, a click on the 'Add Chapter' JMenuItem that is on the customised JPopupMenu?

Thank you for your time.

Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> triggers adding a chapter when clicked.

does the addChapter() method live in the same class as the main gui?

if so, at the end of the method, set the menuItem to enabled
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic