Word Wrap in JEditorPane and System Font
Asked Answered
T

1

5

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.

Twelfth answered 20/5, 2013 at 0:44 Comment(6)
JTextArea and JTextPane word wrap.Metathesize
@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.Twelfth
Use SimpleAttributeSet, for example.Maxinemaxiskirt
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.Metathesize
@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.Twelfth
@Metathesize regular bug, please to see linked post in OPs answer hereLewin
T
9

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)
Twelfth answered 20/5, 2013 at 2:50 Comment(1)
there is an bug in Java7, please to see, with screenshots (in my linked too)Lewin

© 2022 - 2024 — McMap. All rights reserved.