wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Using ItemListener on a JMenu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using ItemListener on a JMenu" Watch "Using ItemListener on a JMenu" New topic
Author

Using ItemListener on a JMenu

Cory Twibell
Greenhorn

Joined: Dec 06, 2001
Posts: 3
I have the following:


<code>
JMenu menu = new JMenu("File");
menu.addItemListener( new ItemListener()
{
public void itemStateChanged( ItemEvent e )
{
JMenu m = (JMenu)e.getItem();
if ( e.getStateChange() == ItemEvent.SELECTED )
{
if ( !valid() ) //error checks a text field
m.setPopupMenuVisible( false );
}
}
} );
</code>


The popup menu still shows. Is JMenu class overriding my call to the popup menu? Is there a different way to do this? Maybe using a different listener?
Thanks
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I'm not sure exactly what you are trying to do, but maybe you just need to use an ActionListener for each MenuItem rather than an ItemListener for the Menu.
HTH
Layne


Java API Documentation
The Java Tutorial
Cory Twibell
Greenhorn

Joined: Dec 06, 2001
Posts: 3
I've tried an ActionListener, but the JMenu doesn't receive the ActionEvent. I am trying to hide the popup menu associated with the JMenu when there is invalid text in a certain text field, because I want the user to fix the text field before moving on.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Perhaps you need to call repaint() to force your Frame to draw itself again?
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Sounds like you should put a listener on the field you are concerned with and toggle setEnabled() on the JMenu. Then the menu would grey out and the user couldn't click on it when there was not valid data in the textfield, and when there was valid data there, the menu would show and behave normally.

-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using ItemListener on a JMenu
 
Similar Threads
JMenu
JMenu problem
A Few Questions
How to create horizontal menus
about JMenu