如何在设置项目的初始值时触发itemStateChanged事件?

时间:2021-07-23 15:23:23

I've implemented a java.awt.event.ItemListener that responds to itemStateChanged. It seems to execute as expected for changes to JComboBox However, the event does not fire for the first value that I assign to the combo box.

我已经实现了一个响应itemStateChanged的java.awt.event.ItemListener。它似乎按预期执行JComboBox的更改但是,事件不会触发我分配给组合框的第一个值。

I couldn't find a way to run this on ideone.com, but the code below can be compiled and executed as-is. Put it in "MyDialog.java," compile with javac.exe *.java and execute with java MyDialog. The DoStuff method will not be executed when the dialog initializes. When the user changes the combo box, however, the event will fire.

我找不到在ideone.com上运行此方法的方法,但下面的代码可以按原样编译和执行。把它放在“MyDialog.java”中,用javac.exe * .java编译并用java MyDialog执行。对话框初始化时,不会执行DoStuff方法。但是,当用户更改组合框时,将触发该事件。

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JDialog;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

public class MyDialog extends JDialog
{   
    public static void main(String[] args)
    {
        MyDialog dlg = new MyDialog();
        dlg.setVisible(true);
    }

    class MyListener implements java.awt.event.ItemListener
    {
        public void itemStateChanged(ItemEvent event)
        {
            if (event.getSource() == myComboBox &&
                event.getStateChange() == ItemEvent.SELECTED )
            {
                DoStuff(event);
            }
        }
    }

    JComboBox myComboBox;

    public MyDialog()
    {
        this.setSize(100,100);

        myComboBox = new JComboBox();
        myComboBox.addItem("Option 1");
        myComboBox.addItem("Option 2");
        this.add(myComboBox);

        MyListener listener = new MyListener();
        myComboBox.addItemListener(listener);

        myComboBox.setSelectedItem("Option 1");

        // EXPECT: DoStuff() was executed
        // GOT: DoStuff() was not executed
    }

    public void DoStuff(java.awt.event.ItemEvent event)
    {
        JOptionPane.showMessageDialog(null, "DoStuff() was reached.");
    }
}

How do I get the DoStuff event to fire when the dialog initializes?

如何在对话框初始化时触发DoStuff事件?

1 个解决方案

#1


It's because "Option 1" is default/selected option already, that's why

这是因为“选项1”已经是默认/选择选项,这就是原因

 myComboBox.setSelectedItem("Option 1");

will not trigger any Item changed event. Change the code to

不会触发任何Item更改事件。将代码更改为

 myComboBox.setSelectedItem("Option 2");

and it will trigger the doStuff() method, because item is indeed changed from current selection.

并且它将触发doStuff()方法,因为item确实已从当前选择中更改。

#1


It's because "Option 1" is default/selected option already, that's why

这是因为“选项1”已经是默认/选择选项,这就是原因

 myComboBox.setSelectedItem("Option 1");

will not trigger any Item changed event. Change the code to

不会触发任何Item更改事件。将代码更改为

 myComboBox.setSelectedItem("Option 2");

and it will trigger the doStuff() method, because item is indeed changed from current selection.

并且它将触发doStuff()方法,因为item确实已从当前选择中更改。