tags:

views:

400

answers:

2

I'm trying to display the contents of an ordered array in something like a JTextField.

for (int i=0; i<array.length; i++) {
    this.textField.setText(array[i]);
}

This won't work for two reasons. The first minor reason: if the array length is 4 then jtextfield is getting it's value reset 4 times rather than appending each element onto the last. Second reason: The JTextField only takes strings. I can't find anything I can use in Swing that will let me display integers to the user. Any help?

A: 

You can concatenate all those integers into a string the then present that value in the textfield.

StringBuilder sb = new StringBuilder();
for( int i : array ) {  // <-- alternative way to iterate the array
     sb.append( i );
     sb.append( ", " );
}

sb.delete(sb.length()-2, sb.length()-1); // trim the extra ","
textField.setText( sb.toString() );

You can use a JTextArea instead of the textfield too.

OscarRyz
You'll need to trim off the extra ", " at the end.
tvanfosson
Ahh yeap.. 1,2,3,4, => 1,2,3,4
OscarRyz
+8  A: 
Johannes Schaub - litb