1

I am trying to implement an ActionListener for a JComboBoxe so that when an item in the list is selected, and the ok jbutton clicked, I want it to appear in a new gui I have defined with a textfield in it therefore when an item is selected从组合框中,它将出现在 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);
    }
});
4

1 回答 1

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

如果你想监听 Co​​mboBox 的变化,你有很多选择,最简单的就是 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???
    }
}
于 2012-08-02T21:53:42.007 回答