通过触发事件在JComboBox中设置setSelectedItem

时间:2022-06-01 18:29:41

I have a JComboBox

我有一个JComboBox

JComboBox tableChoose = new JComboBox();
tableChoose.addItem("Bill");
tableChoose.addItem("Bob");
tableChoose.setSelectedItem("Bill");

and some handling methods

和一些处理方法

public void addComboActionListener(IComboHandler handler){
    tableChoose.addActionListener(handler);
}

public Object getTableChooseSelectedItem(){
    return tableChoose.getSelectedItem();
}

public void actionPerformed(ActionEvent event) {
    JOptionPane.showMessageDialog(null, fileReaderWindow.getTableChooseSelectedItem() , null, JOptionPane.ERROR_MESSAGE);
}

As you see, I set "Bill" as first selected item. When I run the program, I have to reselect "Bill" in order to fire the event in actionPerfomed.

如您所见,我将“Bill”设置为首选项目。当我运行程序时,我必须重新选择“Bill”才能在actionPerfomed中触发事件。

Is there a way to fire the event without reselecting the item in JComboBox? Thank you in advance.

有没有办法在不重新选择JComboBox中的项目的情况下触发事件?先谢谢你。

2 个解决方案

#1


2  

Add the action listener before setting the selected item:

在设置所选项目之前添加动作侦听器:

JComboBox<String> b = new JComboBox<String>();

b.addItem("A");
b.addItem("B");
b.addItem("C");

b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        JComboBox<String> src = (JComboBox<String>) e.getSource();
        System.out.println("ActionListener called. '"+src.getSelectedItem()+"' selected.");
    }
});

b.setSelectedItem("A");

Output:

输出:

ActionListener called. 'A' selected.

#2


1  

Use the actionPerformed() method passing a dummy ActionEvent. For example:

使用actionPerformed()方法传递一个虚拟的ActionEvent。例如:

tableChoose.actionPerformed(new ActionEvent(tableChoose, 0, ""));

the parameter should be ignored and the fireActionEvent() method should be triggered.

应忽略该参数,并应触发fireActionEvent()方法。

Warning this suggestion was devised looking at the source code for JComboBox. The comments state to not call this method directly so you would do this at you own risk as the implementation could change in the future.

警告这个建议是设计查看JComboBox的源代码。注释声明不直接调用此方法,因此您将自行承担风险,因为实施可能在将来发生变化。

Further look at the source code didn't show up any other ways to fire the notification besides calling the setSelectedItem() method.

除了调用setSelectedItem()方法之外,进一步查看源代码没有显示任何其他方法来触发通知。

#1


2  

Add the action listener before setting the selected item:

在设置所选项目之前添加动作侦听器:

JComboBox<String> b = new JComboBox<String>();

b.addItem("A");
b.addItem("B");
b.addItem("C");

b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        JComboBox<String> src = (JComboBox<String>) e.getSource();
        System.out.println("ActionListener called. '"+src.getSelectedItem()+"' selected.");
    }
});

b.setSelectedItem("A");

Output:

输出:

ActionListener called. 'A' selected.

#2


1  

Use the actionPerformed() method passing a dummy ActionEvent. For example:

使用actionPerformed()方法传递一个虚拟的ActionEvent。例如:

tableChoose.actionPerformed(new ActionEvent(tableChoose, 0, ""));

the parameter should be ignored and the fireActionEvent() method should be triggered.

应忽略该参数,并应触发fireActionEvent()方法。

Warning this suggestion was devised looking at the source code for JComboBox. The comments state to not call this method directly so you would do this at you own risk as the implementation could change in the future.

警告这个建议是设计查看JComboBox的源代码。注释声明不直接调用此方法,因此您将自行承担风险,因为实施可能在将来发生变化。

Further look at the source code didn't show up any other ways to fire the notification besides calling the setSelectedItem() method.

除了调用setSelectedItem()方法之外,进一步查看源代码没有显示任何其他方法来触发通知。