2010-10-26 62 views
0
ArrayList list_of_employees = new ArrayList(); 
@Action 
public void reportAllEmployeesClicked(java.awt.event.ActionEvent evt) 
{ 
    this.outputText.setText(""); 
    int i=0; 
    //JOptionPane.showMessageDialog(null,"test Employee list print"); 
    ListIterator list_ir = list_of_employees.listIterator(); //list_of_employees is of  
     //obj type ArrayList 
    while(list_ir.hasNext()) 
     { 
      String o = new String(); 
      o = (String) list_ir.next(); 
      this.outputText.setText(""+o); // this does not work, why? nothing happens 
       //no errors and no output 
      i++; 
      JOptionPane.showMessageDialog(null,o); // this works 
     } 
} 

outputText是嵌套在滾動窗格內的JTextArea類型。 當我設置正常字符串變量的文本輸出顯示,因爲它應該。 作爲循環運行我能夠通過JOptionPane獲得輸出。 存儲在列表中的所有對象都是String對象。 如果有更多信息需要我提供以方便更準確的答案,請告訴我。爲什麼試圖將一個arrayList對象輸出到JtextArea不起作用?

感謝 -Will-

回答

0
// use generics 
List<String> list_of_employees = new ArrayList<String>(); 

// use StringBuilder to concatenate Strings 
StringBuilder builder = new StringBuilder(); 

// use advanced for loop to iterate a List 
for (String employee : list_of_employees) { 
     builder.append(employee).append(" "); // add some space 
} 

// after they are all together, write them out to JTextArea 
this.outputText.setText(builder.toString()); 
0

你可以使用StringBuffer類以及..你可以追加串在一起,並最終使用的ToString()方法。

1
this.outputText.setText(""+o); 

您不應該使用setText(),因爲您將替換現有的文本。因此只會顯示最後一個字符串。

您應該使用:

this.outputText.append(""+o); 
+0

我錯過了在Java API來JTextArea的該TID位。非常感謝。 – 2010-10-27 19:25:09