如果所选项目未更改,JCombobox将不会触发任何操作事件

时间:2023-01-27 21:43:25
JComboBox cb = new JComboBox();
cb.addItem("a");
cb.addItem("b");
cb.addActionListener( new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e);
    }
});
cb.addItemListener( new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent e) {
        System.out.println(e);
    }
});

I am usung OpenJDK (IcedTea 2.3.9), java 1.7.0_21. Neither ActionListener nor ItemListener fire an event, if the selection of a JComboBox has not changed. How can I get JComboBox to fire an event in this case?

我是OpenJDK(IcedTea 2.3.9),java 1.7.0_21。如果没有更改JComboBox的选择,则ActionListener和ItemListener都不会触发事件。在这种情况下,如何让JComboBox触发事件?

1 个解决方案

#1


0  

JComboBox cb = new JComboBox(); cb.addItem("a"); cb.addItem("b"); cb.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { cb_actionPerformed(e); } });

JComboBox cb = new JComboBox(); cb.addItem( “A”); cb.addItem( “B”); cb.addActionListener(new java.awt.event.ActionListener(){public void actionPerformed(ActionEvent e){cb_actionPerformed(e);}});

void cmbAudienceSet_actionPerformed(ActionEvent e) { //Your Code }

void cmbAudienceSet_actionPerformed(ActionEvent e){//您的代码}

Above code worked for me. Can try this. Hope it will work for you as you are expecting.

上面的代码对我有用。可以试试这个。希望它能像你期待的那样对你有用。

#1


0  

JComboBox cb = new JComboBox(); cb.addItem("a"); cb.addItem("b"); cb.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { cb_actionPerformed(e); } });

JComboBox cb = new JComboBox(); cb.addItem( “A”); cb.addItem( “B”); cb.addActionListener(new java.awt.event.ActionListener(){public void actionPerformed(ActionEvent e){cb_actionPerformed(e);}});

void cmbAudienceSet_actionPerformed(ActionEvent e) { //Your Code }

void cmbAudienceSet_actionPerformed(ActionEvent e){//您的代码}

Above code worked for me. Can try this. Hope it will work for you as you are expecting.

上面的代码对我有用。可以试试这个。希望它能像你期待的那样对你有用。