Search code examples
javaswingconsolenewlinejscrollpane

Adding a new line to a JScrollPane


I'm creating a console and would like to know how I can set my console output, a JScrollPane to add multiple lines of text, similar to map.addText, map being the name, yet without adding the charactor number. Instead I want to set the text to be put in the following line so say the console output is like this:

The time is...
Random Text

With the addText code it would only do this:

The time is...Random Text

Not pretty. Of course there is setText, yet that only replaces the contents.

Edit #1: user3152069 was correct in saying map.append would work.


Solution

  • You can't add "lines" to a JScrollPane, nor should you want to. Instead you add lines to whatever component the JScrollPane holds, a very important component, and something you never tell us about, but probably should.

    So take your pick. If it's a JList, then add a new line to the JList's model via the addElement(...) method. If the JScrollPane displays a JTextArea, then append a String to the JTextArea via its append(...) method, but making sure that it contains the \n new line String.


    Note, I'm not sure what you mean by

    similar to map.addText

    What is "map"? What is "addText"? Is it a method? If so, it's not a method I'm familiar with.