JTextPane - dynamic word wrap

16,679

Solution 1

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?

Solution 2

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);
Share:
16,679
user592704
Author by

user592704

Updated on June 04, 2022

Comments

  • user592704
    user592704 almost 2 years

    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?

  • user592704
    user592704 almost 12 years
    because I am interested in text pane dynamic rows++ support. Not the scrolling
  • user592704
    user592704 almost 12 years
    Emm... the issue is about JEditorPane not the JTextPane :( You mean using JEditorPane instead?
  • David Kroukamp
    David Kroukamp almost 12 years
    No the first link does discuss using JTextPanes, however, yes a JEditor could do that too as seen in the 2nd link of the answer
  • David Kroukamp
    David Kroukamp almost 12 years
    @user592704, This link too might help: stackoverflow.com/questions/8980792/…
  • searchengine27
    searchengine27 over 2 years
    -1. Why does this have any upvotes? this doesn't answer the question at all. The answer doesn't even say that it is for a JTextArea and not a JTextPane. Not that it matters because, again, the question has nothing to do with a JTextArea at all.