Soler Mani
Soler Mani

Reputation: 309

How to populate a JTextPane

I have an array of string and would like to add them one by one to the JTextPane, each of which to be separated by a new line. How would I go about achieving this?

Upvotes: 1

Views: 416

Answers (2)

StanislavL
StanislavL

Reputation: 57421

First create line with "\n| chars as separators. Second call

textPane.getDocument().insertString(textPane.getDocument().getLength(), theSumOfStrings, new SimpleAttributeSet());

Upvotes: 1

MByD
MByD

Reputation: 137442

Use the system property for new line:

String separator = System.getProperty( "line.separator" );
StringBuilder sb = new StringBuilder();
for (String s : myStringArray) {
    sb.append(s + separator);
}
myTextPane.setText(sb.toString());

Edit: I found in an old thread, that mentions the usage of the property EndOfLineStringProperty, which makes sense, since the JTextPane extends JEditorPane which uses a document. I would give that a shot. Also, in the JTextPane docs, is written:

For a discussion on how newlines are handled, see DefaultEditorKit.

Upvotes: 1

Related Questions