Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ChangeListener called twice

Status
Not open for further replies.

riches85

Programmer
Nov 13, 2002
59
0
0
US
I have a ChangeListener registered with a JMenuITem that I want to fire whenever a menuItem gets selected but two events are fired. One is fired when the menuItem is selected and one when the menuItem is deselected. Is there a way to only have it fire once when the munuItem is selected? Thanks
 
There's no way to make it fire the event only when the menu item is selected, but you have the following option:
In the stateChanged() method implemented in the ChangeListener object sent as a parameter to yourMenuItem.addChangeListener() method, check for that menu item's isArmed() and isDisplayable() values:
Code:
JMenuItem item = new JMenuItem("item");
item.addChangeListener(new ChangeListener() {
	public void stateChanged(ChangeEvent e) {
		JMenuItem mitem = (JMenuItem) e.getSource();
		if (mitem.isDisplayable() && mitem.isArmed()) {
			System.out.println("ding!");
		}
	}
});

Good luck ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top