2013-03-11 88 views
0
一個ArrayList

我之前問過類似的問題,但意識到眼前的主要問題,我解決不了:添加的方法創建的JComboBox

目前有一個ArrayList名爲SundayList這是儘快裝作爲幀傳遞addStudent被加載(GUI的位)

添加學生類: 編輯

public class AddStudent extends javax.swing.JFrame { 

    public AddStudent() { 
    initComponents(); 
    } 
private void loadLists() throws IOException 
    { 
     //Creating the array of Activities to put into the ComboBoxes 
     File f = new File("Activities.dat"); 

     sundayList = new ArrayList<>(); 
     mondayList= new ArrayList<>(); 
     tuesdayList= new ArrayList<>(); 
     wednesdayList= new ArrayList<>(); 
     thursdayList= new ArrayList<>(); 


try{ 
    BufferedReader reader = new BufferedReader(new FileReader(f)); 

    while(reader.ready()) 
     { 
      String CDay = reader.readLine();        
      String CActivityName = reader.readLine(); 
      String CSupervisor = reader.readLine(); 
      String CLocation = reader.readLine(); 
      String CPaid = reader.readLine(); 
      String nothing = reader.readLine(); 

      if(CDay.equals("Sunday")) 
      { 
       sundayList.add(CActivityName); 
      } 
      else if(CDay.equals("Monday")) 
      { 
       mondayList.add(CActivityName); 
      } 
      else if(CDay.equals("Tuesday")) 
      { 
       tuesdayList.add(CActivityName); 
      } 
      else if(CDay.equals("Wednesday")) 
      { 
       wednesdayList.add(CActivityName); 
      } 
      else if(CDay.equals("Thursday")) 
      { 
       thursdayList.add(CActivityName); 
      }     
    } 
    reader.close(); 
} 
catch (IOException ex) 
{ 
    Logger.getLogger(StartUpFrame.class.getName()).log(Level.SEVERE, null, ex); 
} 
} 
... 
comboboxSunday = new javax.swing.JComboBox(); 
... 
} 



public static void main(String args[]) { 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new AddStudent().setVisible(true); 
      } 
     }); 
    } 

一開始,我試圖調用列表SundayList到組合框的comboboxSunday來填充它,但只得到了找不到符號錯誤。

我需要做些什麼才能做到這一點?

另外,我打算避開我之前見過的MySQL的參與方法,因爲我不熟悉..

爲組合框當前編碼

代碼自動生成通過Netbeans的組合框是:

comboboxSunday = new javax.swing.JComboBox(); 

comboboxSunday.setModel(new javax.swing.DefaultComboBoxModel<>(sundayList.toArray(new String[sundayList.size()]))); 

回答

3

變量SundayList僅限於您的構造函數的範圍。假設您在initComponents方法中創建了JComboBox,您將無法訪問此變量。

可能然而使SundayList一個類成員變量允許你使用變量accross方法。另外最好有加載數據,而不是在用戶界面構造有非UI功能的方法:

public class AddStudent { 
    private List<String> sundayList; 
    private List<String> mondayList; 
    ... 

    private void loadLists() throws IOException { 
     sundayList = new ArrayList<>(); 
     ... 

然後補充:

comboboxSunday.setModel(new DefaultComboBoxModel<>(sundayList.toArray(new String[sundayList.size()]))); 

不要忘了打電話給你新的加載方法:

AddStudent addStudent = new AddStudent(); 
addStudent.loadLists(); 
addStudent.setVisible(true); 

旁白:注意,Java的命名慣例表明,可變開始與小寫這封信會使SundayListsundayList

+0

這意味着我將不得不爲每個列表創建一個類,例如SundayList,MondayList,Tuesday ...等等? – Geuni 2013-03-11 18:38:45

+0

編號使用它們作爲上面顯示的類成員變量 – Reimeus 2013-03-11 18:43:40

+0

我已經根據上面的示例進行了更改,編碼爲「comboboxSunday.setModel(new javax.swing.DefaultComboBoxModel <>(sundayList.toArray(new String [sundayList.size ()])));」由於Netbeans,必須添加javx.swing。但是,仍然存在錯誤,框架(帶有組合框)將不會顯示。我認爲這與添加組合框代碼有關? – Geuni 2013-03-11 19:08:52