Aidan
Aidan

Reputation: 53

Append Text to JTextArea on Multiple Lines?

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

Answers (3)

pbespechnyi
pbespechnyi

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

Hovercraft Full Of Eels
Hovercraft Full Of Eels

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

PeerNet
PeerNet

Reputation: 855

put new line character which is \n at the end of the string

Upvotes: 6

Related Questions