2013-03-21 39 views
0

我有一個名爲columnsArray []的數組,它被預定義爲包含6個字符串。當我運行我的方法列()時,它應該用一個新的字符串數組覆蓋columnsArray [],用戶通過選中框進行選擇。 我試圖實現這一點的方式添加每個框檢查到一個arrayList,然後將arrayList轉換爲數組[]。但是,當代碼運行時,columnsArray不會被覆蓋。覆蓋預定義的數組

這裏是我到目前爲止的代碼:

public class EditView { 
    private JFrame frame; 
    JCheckBox appNo, name, program, date, pName, country, fileLoc, email, uni, 
      countryUni, degree, classification, funding, supervisor, 
      rejectedBy, misc; 
    public ArrayList<String> columnArrLst; 
    public String[] columnsArray = { "Application Number", "Name", "Program", 
      "Date", "Project Name", "Country of Uni" }; 

    public EditView() { 

    } 

    public void makeFrame() { 
     frame = new JFrame("Edit View"); 
     frame.setPreferredSize(new Dimension(300, 350)); 
     Container contentPane = frame.getContentPane(); 
     contentPane.setLayout(new GridLayout(0, 2, 20, 20)); 

     appNo = new JCheckBox("appNo"); 
     name = new JCheckBox("Name"); 
     program = new JCheckBox("Program"); 
     date = new JCheckBox("Date"); 
     pName = new JCheckBox("Project Title"); 
     country = new JCheckBox("Country of Origin"); 
     fileLoc = new JCheckBox("Current File Location"); 
     // countryRef = new JCheckBox(""); 
     email = new JCheckBox("Email address"); 
     uni = new JCheckBox("Last University"); 
     countryUni = new JCheckBox("Country of last Uni"); 
     degree = new JCheckBox("Degree"); 
     classification = new JCheckBox("Degree Classification"); 
     funding = new JCheckBox("funding"); 
     supervisor = new JCheckBox("Supervisor"); 
     rejectedBy = new JCheckBox("Rejected By"); 
     misc = new JCheckBox("Miscelaneous"); 
     contentPane.add(appNo); 
     contentPane.add(name); 
     contentPane.add(program); 
     contentPane.add(date); 
     contentPane.add(pName); 
     contentPane.add(country); 
     contentPane.add(fileLoc); 
     contentPane.add(email); 
     contentPane.add(uni); 
     contentPane.add(countryUni); 
     contentPane.add(degree); 
     contentPane.add(classification); 
     contentPane.add(supervisor); 
     contentPane.add(rejectedBy); 
     contentPane.add(misc); 

     JButton changeView = new JButton("Change View"); 

     changeView.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       columns(); 
       frame.dispose(); 
      } 
     }); 

     contentPane.add(changeView); 
     frame.pack(); 
     frame.setVisible(true); 
     frame.setLocationRelativeTo(null); 
    } 

    public String[] columns() { 
     columnArrLst = new ArrayList<String>(); 
     if (appNo.isSelected()) { 
      columnArrLst.add("AppNo"); 
     } 
     if (name.isSelected()) { 
      columnArrLst.add("Name"); 
     } 
     if (date.isSelected()) { 
      columnArrLst.add("Date"); 
     } 
     if (fileLoc.isSelected()) { 
      columnArrLst.add("file Location"); 
     } 
     if (country.isSelected()) { 
      columnArrLst.add("Country"); 
     } 
     if (email.isSelected()) { 
      columnArrLst.add("Email"); 
     } 
     if (uni.isSelected()) { 
      columnArrLst.add("University"); 
     } 
     if (countryUni.isSelected()) { 
      columnArrLst.add("Country of Uni"); 
     } 
     if (degree.isSelected()) { 
      columnArrLst.add("Degree"); 
     } 
     if (classification.isSelected()) { 
      columnArrLst.add("Degree Classification"); 
     } 
     if (pName.isSelected()) { 
      columnArrLst.add("ProjectName"); 
     } 
     if (funding.isSelected()) { 
      columnArrLst.add("Funding"); 
     } 
     if (supervisor.isSelected()) { 
      columnArrLst.add("Supervisor"); 
     } 
     if (rejectedBy.isSelected()) { 
      columnArrLst.add("rejected By"); 
     } 
     if (misc.isSelected()) { 
      columnArrLst.add("Miscelaneous"); 
     } 
     columnsArray = new String[columnArrLst.size()]; 
     columnArrLst.toArray(columnsArray); 
     return columnsArray; 
    } 
} 

爲什麼它不覆蓋任何想法?謝謝你的幫助。

+1

因爲它應該是'columnsArray = columnArrLst.toArray(columnsArray);' – OldCurmudgeon 2013-03-21 12:23:58

+1

[This](http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList。 html#toArray(T []))將有助於 – 2013-03-21 12:25:51

+0

@OldCurmudgeon只需'columnArrLst.toArray(columnsArray);'爲我工作。從規範中返回一個數組,其中包含該列表中的所有元素(從第一個元素到最後一個元素);返回數組的運行時類型是指定數組的運行時類型,如果列表適合指定的數組, ,否則返回一個新的數組,並且指定數組的運行時類型和這個列表的大小。「 – Averroes 2013-03-21 12:33:00

回答

3

試試這個

columnsArray = columnArrLst.toArray(new String[columnArrLst.size()]); 

希望它能幫助。

+0

謝謝大家。那個班現在工作正常。看來問題在於另一個班級。所以我現在就開始研究。我非常感謝所有的幫助! – Hoggie1790 2013-03-21 12:39:26

1

用以下替換第二行[columnArrLst.toArray(columnsArray);]。

columnsArray = columnArrLst.toArray(columnsArray);

1

沒有什麼不對您的代碼,數組的內容,實際上改變,當你使用它作爲

EditView e = new EditView(); 
e.makeFrame(); 

columnArrLst.toArray(columnsArray); 

請注意,您的JFrame顯示在插入打印循環不同的線程。如果你想檢查這些值,你需要明確地等待,直到按下按鈕看到它們被更改。如果你正在做的事情,如:

EditView e = new EditView(); 
e.makeFrame(); 
for (String s : e.columnsArray) { System.out.println(s);} 

這將打印舊的值,因爲印刷線實際上是一個不同的,並立即打印出數值。