• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using ItemListener on a JMenu

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Cory Twibell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps you need to call repaint() to force your Frame to draw itself again?
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic