动作听众不工作

时间:2012-08-02 11:16:09

标签: java swing action actionlistener

我正在尝试为JComboBoxe实现一个ActionListener,这样当选择列表中的一个项目并单击ok jbutton时,我希望它出现在我用其中的文本字段定义的新gui中,因此当从组合框中选择项目,它将出现在gui的文本字段中,以及选择项目的详细信息。

此示例显示了一个组合框,但我总共有6个。

jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jComboBox4.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        jComboBox4MouseClicked(evt);
    }
});

1 个答案:

答案 0 :(得分:0)

首先将ActionListener添加到所需按钮

// When the button is clicked this is called...
public class ButtonActionListener extends ActionListener {
    public void actionPerformed(ActionEvent evt) {
        Object value = comboBox.getSelectedItem();
        // check for null value
        // do what ever it is you want to do after that...            
    }
}

如果你想监视对ComboBox的更改,你有很多选择,最简单的是ActionListener

// When the button is clicked this is called...
public class ComboBixActionListener extends ActionListener {
    public void actionPerformed(ActionEvent evt) {
        Object value = comboBox.getSelectedItem();
        // The combo box value has changed, maybe update the text field???
    }
}