Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is itemStateChanged on JComboBox is called twice when changed?

I'm using a JComboBox with an ItemListener on it. When the value is changed, the itemStateChanged event is called twice. The first call, the ItemEvent is showing the original item selected. On the second time, it is showing the item that has been just selected by the user. Here's some tester code:

public Tester(){      JComboBox box = new JComboBox();     box.addItem("One");     box.addItem("Two");     box.addItem("Three");     box.addItem("Four");      box.addItemListener(new ItemListener(){         public void itemStateChanged(ItemEvent e){             System.out.println(e.getItem());         }     });      JFrame frame = new JFrame();     frame.getContentPane().add(box);     frame.pack();     frame.setVisible(true); } 

So when I changed the Combo box once from "One" to "Three" the console shows:

One Three 

Is there a way I can tell using the ItemEvent maybe, that it's the second item (ie. the user selected item)? And if someone can explain why it gets called twice, that would be nice too!

Thanks

like image 400
Nicks Avatar asked Dec 01 '08 11:12

Nicks


People also ask

What is itemStateChanged?

itemStateChanged. void itemStateChanged(ItemEvent e) 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).

What is the main difference between JComboBox and Jlistbox?

A JComboBox is a component that displays a drop-down list and gives users options that we can select one and only one item at a time whereas a JList shows multiple items (rows) to the user and also gives an option to let the user select multiple items.

Which is the description of a JComboBox?

JComboBox is a part of Java Swing package. JComboBox inherits JComponent class . JComboBox shows a popup menu that shows a list and the user can select a option from that specified list . JComboBox can be editable or read- only depending on the choice of the programmer .

Which listener is implemented for JComboBox?

Adds a PopupMenu listener which will listen to notification messages from the popup portion of the combo box. Initializes the editor with the specified item. Sets the properties on this combobox to match those in the specified Action . This method is public as an implementation side effect.


2 Answers

Have a look at this source:

import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class Tester {      public Tester(){          JComboBox box = new JComboBox();         box.addItem("One");         box.addItem("Two");         box.addItem("Three");         box.addItem("Four");          box.addItemListener(new ItemListener(){             public void itemStateChanged(ItemEvent e){                 System.out.println(e.getItem() + " " + e.getStateChange() );             }         });          JFrame frame = new JFrame();         frame.getContentPane().add(box);         frame.pack();         frame.setVisible(true);     }      public static void main(String [] args) {         Tester tester = new Tester();     } } 

Use the getStateChange to determine if an item is selected or deselected

like image 135
kgiannakakis Avatar answered Sep 16 '22 20:09

kgiannakakis


According to this thread,

It gets tripped when you leave one result and then called again when set to another result

Don't listen for itemStateChanged. Use an ActionListener instead, which is good for handling events of the combo.
You need a ItemStateListener if you need to separately handle deselection / selection depending on the item involved.

Changing the state of the item within itemStateChanged causes itemStateChanged to be fired... this called "reentrance".

like image 21
VonC Avatar answered Sep 18 '22 20:09

VonC