2014-11-24 43 views
1

的我創建的JComboBoxArrayList的JComboBoxes

private static ArrayList<JComboBox> comboList = new ArrayList<>(); 

的ArrayList,然後加入JComboBox每個實例的ArrayList

private void courseUnit() { 
     String[] units = {"6", "5", "4", "3", "2", "1"}; 
     int x = 520, y = 30; 
     for (int i = 0; i < 10; i++) { 
      comboUnits = new JComboBox<>(units); 
      comboUnits.setBounds(x, y, 100, 25); 
      y += 30; 
      comboUnits.setToolTipText("Select course unit"); 
      comboUnits.setVisible(true); 
      comboUnits.addActionListener(new PaneAction()); 
      add(comboUnits); 
      comboList.add(comboUnits); //comboUnits added to ArrayList 
     } 
    } 

我的問題是,我如何得到每個組合框的selectedItem屬性因爲我試過這個ArrayList

//this is supposed to get the selected item of the first ComboBox and assign to courseGrade[0] 
    public void actionPerformed(ActionEvent evt) { 
      String[] courseGrade = new String[10]; 
      courseGrade[0] = (String)comboList.get(0).getSelectedItem(); 

and the pr圖沒有編譯。

+0

郵政編譯錯誤 – mavroprovato 2014-11-24 12:39:28

+0

java.lang.ExceptionInInitializerError 引起:java.lang.NullPointerException – 2014-11-24 12:42:01

+0

這不是一個編譯錯誤,那是一個你的代碼拋出異常。你應該檢查堆棧跟蹤,它應該告訴你'NullPointerException'發生在哪裏。你忘了在那條線上初始化一些東西。 – mavroprovato 2014-11-24 12:47:46

回答

0

可以應重視ActionListner,而你所添加的JComboBoxArrayList

private void courseUnit() { 
      String[] units = {"6", "5", "4", "3", "2", "1"}; 
      int x = 520, y = 30; 
      for (int i = 0; i < 10; i++) { 
       comboUnits = new JComboBox<>(units); 
       comboUnits.setBounds(x, y, 100, 25); 
       y += 30; 
       comboUnits.setToolTipText("Select course unit"); 
       comboUnits.setVisible(true); 
       //comboUnits.addActionListener(new PaneAction()); 
       add(comboUnits); 

//////////////// Here are the changes 

       comboUnits.addActionListener (new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         String selectedItem = (String)e.getSelectedItem(); 
        } 
       }); 
       comboList.add(comboUnits); //comboUnits added to ArrayList 
      } 
     } 
+0

靜態方法e.getItem在ActionEvent中不存在,但我想你的意思是e.getSelectedItem(); – 2014-11-24 14:15:16

+0

答覆已更新。如果您發現我的答案有幫助,請將其標記爲正確。 – Junaid 2014-11-24 15:08:34

+0

謝謝。這很有幫助 – 2014-11-24 17:13:03

0

我終於得到它。我不得不創建ArrayList的一個靜態實例變量

private static ArrayList<JComboBox> comboList; 

,然後實例化,它在構造函數中

comboList = new ArrayList<>(); 

新courseUnit()方法現在看起來是這樣

private void courseUnit() { 
     String[] units = {"6", "5", "4", "3", "2", "1"}; 
     int x = 520, y = 30; 
     for (int i = 0; i < 10; i++) { 
      comboUnits = new JComboBox<>(units); 
      comboUnits.setBounds(x, y, 100, 25); 
      comboUnits.setToolTipText("Select course unit"); 
      y += 30; 
      comboUnits.setVisible(true); 
      add(comboUnits); 
      //I added ActionListener to each comboBox in the ArrayList, 
      //thanks to Junaid for this 
      comboUnits.addActionListener(new ActionListener(){ 
       public void actionPerformed(ActionEvent evt){  
        ArrayList<String> courseGrade = new ArrayList<>(); 
        JComboBox comboBox = (JComboBox) evt.getSource(); 
        courseGrade.add((String) comboBox.getSelectedItem()); 
       } 
      }); 
      comboList.add(comboUnits); 
     }//end of loop 
    } //end of method