File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JPopupMenu JMenuItem and JMenuItem : Listen to each other? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPopupMenu JMenuItem and JMenuItem : Listen to each other?" Watch "JPopupMenu JMenuItem and JMenuItem : Listen to each other?" New topic

JPopupMenu JMenuItem and JMenuItem : Listen to each other?

D R Wilkinson
Ranch Hand

Joined: Oct 29, 2004
Posts: 51
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.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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
I agree. Here's the link:
subject: JPopupMenu JMenuItem and JMenuItem : Listen to each other?
It's not a secret anymore!