2012-08-02 51 views
1

我想实现一个JComboBoxe一个ActionListener,这样当选择列表中的项目,并确定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); 
    } 
}); 
+2

如果你想在JButton被单击时发生某些事情,那么将你的ActionListener附加到JButton,而不是JComboBox。 – 2012-08-02 11:21:20

+0

里面的'的actionPerformed(...)''的JButton的(OK按钮)的方法',只需使用'jComboBox4.getSelectedItem()等为others'并简单地通过它,在其它部件被显示,由于需要的话:-) – 2012-08-02 13:17:48

+0

非常感谢您的帮助,但是这似乎并没有工作,我想他们的指示在这两个地方的JButton 1是主键或otherwords的(OK按钮 – user1571125 2012-08-02 20:22:56

回答

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

如果要监视更改组合框,您有很多的选择,最简单的就是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??? 
    } 
} 
+0

再次很多感谢,但仍然无法得到它无论是我说它不会改变当项目点击组合框到细节的文本字段gui是有关私人或公共新的netbeans试图找到我的脚与它 – user1571125 2012-08-03 11:31:22