-4
所以我有this項目,JComboBox的addActionListener方法不起作用
的源代碼是here。
當您運行該項目並轉到處理時,那裏有一個jcombobox
假設有一個addActionListener。
p_customer_list = new JComboBox<>(customers_name);
pp_customer_list.setPreferredSize(new Dimension(360, 35));
panel_processing_header.add(pp_customer_list);
//pp_customer_list.addActionListener(this);
pp_customer_list.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox tmpBox = (JComboBox) e.getSource();
int selected = tmpBox.getSelectedIndex();
pp_refresh_data(selected);
}
});
這是我到目前爲止,它的假設找到所選索引時的組合框的值更改並將其傳遞給pp_refresh_data()
但出於某種原因,它不運行(我試圖把一個JOptionPane
看當代碼執行時,它只在程序運行時執行一次)。
當你知道它是pp_customer_list時,爲什麼你會得到ActionEvent的來源?只需製作JComboBox final並在偵聽器中使用它。也請不要使用帶下劃線的名字,除非所有的字母都大寫。否則,只需使用像pCustomerList這樣的東西。 – 2013-03-25 13:17:59
@AliAlamiri從事件中獲取源代碼(適當地防範classCastException)是A-Good-Idea - 解耦代碼。 – kleopatra 2013-03-25 13:29:31
無關:[不要使用setXXSize,ever](http://stackoverflow.com/a/7229519/203657) – kleopatra 2013-03-25 13:30:08