The moose likes Swing / AWT / SWT / JFace and the fly likes Using ItemListener on a JMenu Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply 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: 4120

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.
 
IntelliJ Java IDE
 
subject: Using ItemListener on a JMenu
 
Threads others viewed
A Few Questions
JMenu problem
JMenu
How to create horizontal menus
about JMenu
developer file tools