Question

I have an ArrayList containing Strings called responseList:

ArrayList<String> responseList;

I also have a JTextField called responseField:

JTextField responseField;

The ArrayList is populated with varying lines of plaintext strings. For instance, responseList.get(0) would contain something along the lines of:

cat mouse hello world
dog kitten puppy potatoes
carrot beef kryptonite

Simple enough. However, this is where the discrepancy occurs. When I do:

System.out.println(responseList.get(0));

I get, as expected:

cat mouse hello world
dog kitten puppy potatoes
carrot beef kryptonite

Strangely enough, when I do:

 responseField.setText(responseList.get(0));

the responseField element in my Swing GUI displays it as:

cat mouse hello worlddog kitten puppy potatoescarrot beef kryptonite

The new line characters don't seem to be read in. JTextField is interpreting my original text differently than Eclipse's console output does.

Can I get some explanation on why JTextField's setText(String) strips my text of new lines? And further, how to remedy this issue?

Thanks in advance.

Was it helpful?

Solution

Set the responseField variable to be a JTextArea instead of JTextField: a JTextField contains, by definition, only one line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top