JTextPane - dynamic word wrap

user592704 picture user592704 · Jul 2, 2012 · Viewed 15.7k times · Source

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?

Answer

David Kroukamp picture David Kroukamp · Jul 2, 2012

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?