Reputation: 53
I have a string with multiple numbers, when printed out it looks something like this:
2
4
5
10
20
25
50
However, when I append the string to a JTextArea it looks like this:
24510202550
How can I make the JTextArea look like the ouput with the numbers on seperate lines? Thanks!
Upvotes: 2
Views: 1143
Reputation: 2291
You're probably using System.out.println()
to print to console. The System.out.println()
will add '\n'
character to the end of each line for you.
But to output strings to JTextArea
in same way use jTextArea.append('\n');
.
Upvotes: 2
Reputation: 285403
Your JTextArea extends JTextComponent and thus has its own read(...)
method that allows it to read in text files (among other things), understand them in an OS-dependent manner, and then display them, complete with new-lines. For example, please see this code which is essentially,
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
textArea.read(br, null); // here we read in the text file
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {}
}
}
Upvotes: 3