开发者

Why does this attempt to output an arrayList object to a JtextArea not work?

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 is of JTextArea type nested inside of a scrolling pane. when i set text with normal String variables the output appears as it should. as the loop runs i am able to obtain output through the JOptionPane. All objects stored in the list are String objects. if there is any more info i need to provide to facilitate a more accurate answer, let me know.

开发者_JAVA百科

Thanks -Will-


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

You should not be using setText() since you will replace the existing text. Therefore only the last string will appear.

You should be using:

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


// 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());


you can use StringBuffer class as well.. you can append the strings together and finally use a .toString() method .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜