1

I'm not too familiar with Java GUI programming, and I wanted to do something where I have a loop that spits out a list of stuff and have the JTextField render it in the order it comes out.

I just do not know how the second parameter of the JTextField insert() function works. Right now when I do something like:

for(int i = 0; i < list.size(); i++){
    textArea.insert(list.get(i), 0);
}

It does what I want, except it lists everything in backwards order that I put it in. I want it to display everything the other way around.

Thank you for any advice.

KWJ2104
  • 1,959
  • 6
  • 38
  • 53
  • See also [`Console`](http://stackoverflow.com/questions/4443878/redirecting-system-out-to-jtextpane/4444677#4444677). – trashgod Oct 31 '11 at 03:41
  • 1
    *"Use JTextField to List items"* No, I'd Use `JList` (or possibly `JTable` or `JTree` depending on the complexity and associations between the items) to List items. – Andrew Thompson Oct 31 '11 at 04:08
  • api doc: "Inserts the specified text at the specified position" - which exact part of that sentence don't you understand? – kleopatra Oct 31 '11 at 09:35
  • btw, you are using a _JTextArea_ , aren't you? – kleopatra Oct 31 '11 at 09:39

1 Answers1

1

All you need to define a temporary string, result and for every item in the list add the string representation to that variable. When you have looped through everything, all you need to do is textArea.setText(result).

String result = "";    
for(int i = 0; i < list.size(); i++)
{
    result += list.get(i).toString();
}

textArea.setText(result);
jez
  • 1,239
  • 1
  • 7
  • 14
  • Is this the only way I can do this? I wanted to have a textfield kind of similar to a command line output and have the user be able to make a bunch of the lists etc. This would mean that I would be constantly updating a really long string. – KWJ2104 Oct 31 '11 at 03:32
  • 3
    See [`append()`](http://download.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html#append%28java.lang.String%29). – trashgod Oct 31 '11 at 03:34
  • That one's new to me as well! Looks like I've got some code to edit! – jez Oct 31 '11 at 03:40