2013-03-25 88 views
-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看當代碼執行時,它只在程序運行時執行一次)。

+0

當你知道它是pp_customer_list時,爲什麼你會得到ActionEvent的來源?只需製作JComboBox final並在偵聽器中使用它。也請不要使用帶下劃線的名字,除非所有的字母都大寫。否則,只需使用像pCustomerList這樣的東西。 – 2013-03-25 13:17:59

+1

@AliAlamiri從事件中獲取源代碼(適當地防範classCastException)是A-Good-Idea - 解耦代碼。 – kleopatra 2013-03-25 13:29:31

+0

無關:[不要使用setXXSize,ever](http://stackoverflow.com/a/7229519/203657) – kleopatra 2013-03-25 13:30:08

回答

1

很難從部分代碼片段中知道,但是您是否有2個組合,一個名爲「p_customer_list」,另一個名爲「pp_customer_list」?

這可能是你的問題。您可能會將聽衆添加到錯誤的組合中,或者您可能會將錯誤的組合添加到面板,或者您可能不需要兩個,或者可能...

同樣,很難從片段。