2012-08-02 61 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