4

I'm wanting to create a large text field for a user to type something in. I also want it to use the default system font to match the look and feel that is expected. So I tried to use a JEditorPane with the default constructor, which uses plain text encoding.

JEditorPane editorPane = new JEditorPane();
editorPane.setText(gettysburgAddress);

enter image description here

The trouble with this is that plain text encoding doesn't wrap to a newline at the end of each word, only when it runs out of characters.

I tried using the HTML encoding, which word wraps:

JEditorPane editorPane = new JEditorPane("text/html", "");
editorPane.setText(gettysburgAddress);

enter image description here

This has the word wrap, but it defaults to a different font than the default for the system (Helvetica on Mac OS X, which I don't want.

How can I get the best of both worlds: word wrap and the system default font? I don't need any special formatting or anything for this, so plain text encoding will do if it is possible.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Thunderforge
  • 19,637
  • 18
  • 83
  • 130
  • JTextArea and JTextPane word wrap. – camickr May 20 '13 at 00:45
  • @camickr, that'll probably work. Turn it into an answer showing it (perhaps also showing how this could alternatively be done with a JEditorPane) and I'll accept it. – Thunderforge May 20 '13 at 00:57
  • Use `SimpleAttributeSet`, for [example](http://stackoverflow.com/a/15600689/230513). – trashgod May 20 '13 at 01:16
  • `Turn it into an answer showing it` - What? I'm not going to write the code for you. If you tried it on your own you should have your answer by now. – camickr May 20 '13 at 01:56
  • @camickr, I figured you'd want the reputation for an answer; that's what I meant by "turn it into an answer showing it". If you're not interested, I can write the answer myself. – Thunderforge May 20 '13 at 02:31
  • @camickr regular bug, please to see linked post in OPs answer here – mKorbel May 20 '13 at 07:40

1 Answers1

7

If all that is needed is a word-wrapped JEditorPane using the system font, and you don't need anything special like stylized text or images, then it's probably best just to switch to a JTextArea, which is a text component for doing just plain text. It doesn't word wrap by default, but it's easy to make it happen:

JTextArea textArea = new JTextArea();
textArea.setLineWrap(true); //Makes the text wrap to the next line
textArea.setWrapStyleWord(true); //Makes the text wrap full words, not just letters
textArea.setText(gettysburgAddress);

If you absolutely must use a JEditorPane for some reason, you'll have to roll up your sleeves and make some changes to the way that the JEditorPane is rendering text. On the plus side, you have several different methods to choose from. You can:

  • Set the encoding to HTML (which word wraps) and use CSS to specify the font (described here)
  • Set the encoding to RTF (which word wraps) and modify the font values of the underlying RTFEditorKit (described here)
  • Create a SimpleAttributeSet and use it when adding strings to specify that they should be displayed in that way (described here)
Community
  • 1
  • 1
Thunderforge
  • 19,637
  • 18
  • 83
  • 130
  • there is an bug in Java7, [please to see](http://stackoverflow.com/questions/14217911/jtextpane-is-not-wrapping-text/14218102#14218102), with screenshots [(in my linked too)](http://stackoverflow.com/a/11001972/714968) – mKorbel May 20 '13 at 07:37