ItemListener calls Twice

Hi,

I am having a ComboBox and when I select an Item, my listener calls twice.

cmbInputSource.addItemListener(new java.awt.event.ItemListener(){

publicvoid itemStateChanged(ItemEvent e){

setInputSource(cmbInputSource.getSelectedItem().toString());

}

});

Thats my code, I am using this code in my Constructor, but I still dont find why this method calls twice.

Any Ideas?

Thnx

[643 byte] By [mraheela] at [2007-10-2 1:49:01]
# 1
You need to inspect the state change:if (e. getStateChange() == ItemEvent.SELECTED) {...}
MartinHilperta at 2007-7-15 19:30:14 > top of Java-index,Java Essentials,Java Programming...
# 2

Annnnnnd, from the java docs:

javax.swing.JComboBox#addItemLitener(ItemListener):

Adds an ItemListener.

aListener will receive one or two ItemEvents when the selected item changes.

Lets click the link and find out a bit more:

ItemListener#itemStateChanged(ItemEvent)

Invoked when an item has been selected or deselected by the user. The code written for this method performs the operations that need to occur when an item is selected (or deselected).

Does that clear it up?

MartinHilperta at 2007-7-15 19:30:14 > top of Java-index,Java Essentials,Java Programming...
# 3
Thnx guys, its really a lot of help.
mraheela at 2007-7-15 19:30:14 > top of Java-index,Java Essentials,Java Programming...
# 4

No problem...

Try and get in to the habbit of making the JavaDocs your first port-of-call for this sort of problem though. The answers are nearly always there (and they certainly were in this case - as my post above shows).

You'll generally find the solution to your problem much quicker by taking that approach.

~D

mraheela at 2007-7-15 19:30:14 > top of Java-index,Java Essentials,Java Programming...
# 5

Great job 枲we define this not a bug, but a feature :/

Or is anyone serious that it is good API design to make a method name look self-explanatory, but have an important detail hidden in the documentation?

The unnecccessary double call of event listeners may potentially decrease the perceived performance of the system by 50% 枲this shouldn't be a favourite solution.

I searched the bug database for an "RFE" concerning this point, didn't find one. However I don't file one, as I don't have a simple test code at hand to demonstrate the problem.

Martin

martinr2a at 2007-7-15 19:30:14 > top of Java-index,Java Essentials,Java Programming...
# 6
martinr2 - why do you believe this needs a RFE?The whole point of itemListeners is to tell you about selection and deselection. If you only want to know about selection, use an ActionListener instead.
TimRyanNZa at 2007-7-15 19:30:14 > top of Java-index,Java Essentials,Java Programming...
# 7

It's not a matter of selection and deselection - including deselection events, in some circumstances there are actually four events for one click. Which was not the case for java.awt.Choice, so better don't migrate from AWT to Swing the cookbook way.

Like I said, I unfortunately don't have a simple example to illustrate this, but I interpret the javadoc comment as a sign that the people at Javasoft are aware of this.

martinr2a at 2007-7-15 19:30:14 > top of Java-index,Java Essentials,Java Programming...