4

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?

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
user592704
  • 3,674
  • 11
  • 70
  • 107

3 Answers3

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?

Community
  • 1
  • 1
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • Emm... the issue is about JEditorPane not the JTextPane :( You mean using JEditorPane instead? – user592704 Jul 02 '12 at 19:58
  • 2
    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 Jul 02 '12 at 19:59
  • @user592704, This link too might help: http://stackoverflow.com/questions/8980792/is-it-possible-for-jtextpane-to-have-columns-and-rows – David Kroukamp Jul 02 '12 at 20:03
1

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);
xorissao
  • 35
  • 4
  • 3
    -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. – searchengine27 Oct 11 '21 at 18:50
0

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()

Chad
  • 872
  • 8
  • 24