user592704
user592704

Reputation: 3704

JTextPane - dynamic word wrap

I tried to use jTextPane1.setText("xxx xxxx xxx xxxxx xx xxx xxxx xxx etc..."); but JTextPane does not word wrap it at all showing all the text in one line only instead. It would be interesting to support word wrap on jTextPane1 resized too...

So my question is... how to make JTextPane support word wrap?

Upvotes: 4

Views: 20132

Answers (3)

xorissao
xorissao

Reputation: 33

I had the same problem, the solution from David Kroukamp was very helpful. I changed it to JTextArea and set the following properties, as described in this tutorial:

    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);

Upvotes: 0

David Kroukamp
David Kroukamp

Reputation: 36423

Try using a JTextArea and call setWrapStyleWord(true); on its instance this should do what you need.

EDIT:

If you need to use a JTextPane as a requirement(which you said you do), then have a look at a similar question that I found which answer should be of help: How is word-wrapping implemented in JTextPane, and how do I make it wrap a string without spaces?

Upvotes: 2

Chad
Chad

Reputation: 892

Why not use a JTextArea instead of a Pane?

http://docs.oracle.com/javase/1.5.0/docs/api/

public void setWrapStyleWord(boolean word)

Sets the style of wrapping used if the text area is wrapping lines. If set to true the lines will be wrapped at word boundaries (whitespace) if they are too long to fit within the allocated width. If set to false, the lines will be wrapped at character boundaries. By default this property is false. Parameters: word - indicates if word boundaries should be used for line wrapping See Also: getWrapStyleWord()

Upvotes: 0

Related Questions