Why is itemStateChanged on JComboBox is called twice when changed?

64,533

Solution 1

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

Solution 2

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".

Solution 3

I wanted to get the index string after selected and set in combobox

        comboBox1.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED) {
                    comboBox1ItemStateChanged();
                }
            }
        });

Solution 4

private void dropDown_nameItemStateChanged(java.awt.event.ItemEvent evt) {                                                 


    if(evt.getStateChange() == ItemEvent.SELECTED)
    {
        String item = (String) evt.getItem();
        System.out.println(item);
    }

}

Good Luck!

Solution 5

The code is:

public class Tester {

    private JComboBox box;

    public Tester() {

        box = new JComboBox();
        box.addItem("One");
        box.addItem("Two");
        box.addItem("Three");
        box.addItem("Four");

        box.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == 1) {

                    JOptionPane.showMessageDialog(box, e.getItem());
                    System.out.println(e.getItem());
                }
            }
        });

        JFrame frame = new JFrame();
        frame.getContentPane().add(box);
        frame.pack();
        frame.setVisible(true);
    }
}
Share:
64,533
Nicks
Author by

Nicks

Updated on July 09, 2022

Comments

  • Nicks
    Nicks almost 2 years

    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

  • Brett
    Brett over 15 years
    It's generally good practice to ignore the event and look to see what the actual state is.
  • BJ Dela Cruz
    BJ Dela Cruz over 12 years
    Your answer really helped! Thank you so much!
  • gumuruh
    gumuruh about 12 years
    this is not answering the question. IT still getting the same result: two event occured.
  • gumuruh
    gumuruh about 12 years
    you may also take note, that if : We do removeAll() method from a ComboBox the result is that it will also calling twice....
  • Martin
    Martin almost 6 years
    Note that if the user selects the same value as previously selected, an ActionListener will still be fired which doesn’t happen with an ItemListener.
  • FonzTech
    FonzTech over 5 years
    It's better to use ItemEvent.SELECTED instead of 1