Word Wrap in JEditorPane and System Font - java

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);
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);
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.

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)

Related

In a Java JTextPane, how do I make the font style not reset to the small unstyled font when the user backspaces back to the edge of the text pane?

I have a JTextPane which I use for html styled editing. I have one problem which is not exactly critical, but would be nice if I could fix it. The problem is that when I backspace in the text pane up to the end of the line, the style text I had is reset back to the default font and style for the text pane.
How do I stop this from happening and retain the styling.
I have tried setting the JTextPane with
"<html><head></head><body>" +
"<p style = \"font-size:22; font-family:sans-serif\"></p>" +
"</body></html>";
This works until I save or refresh the html. Once it is saved or refreshed all the text returns back to the very tiny JTextPane font and all styling is removed.
How do I make it retain the styling when saving, refreshing, or backspacing?
Update: I have tried doing this, but it only works until I save the text in the text pane and update it. I tested this in java 1.7 like the link said to do.

Set fonts for individual strings?

So, I have a JTextArea in my program, and I'm appending some text into in.
I'm trying to make just one of the appended Strings bold, without making all of the text in the JTextArea bold. There doesn't seem to be a way, at least not one that I can find, to edit the font of a String without adding it to something like a JLabel (which I'd rather not do).
Anyone know a work around for this?
Thanks for your time.
The best work around: don't use a JTextArea, a component that is made to display simple single fonted text easily. Instead use one of the more robust text components such as JEditorPane or JTextPane. Please have a look at the tutorials:
How to use Editor Panes and Text Panes
Using Text Components
Use a JTextPane instead of JTextArea. By embedding text as html in this component you have more freedom to style the text. See this example:
JTextPane myTextPane = new JTextPane();
myTextPane.setContentType("text/html");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<html>");
stringBuilder.append("<b>bold text </b>");
stringBuilder.append("normal text");
stringBuilder.append("</html>");
myTextPane.setText(stringBuilder.toString());

Append error message on jTextArea with different color

I was wondering if there is a way to append an error message in a jTextArea with different font color.
Like this it will make a difference between the normal output that I am appending and the error output.
For example, on Netbeans the system.err font color is red, and the System.out is appearing black.
JTextArea only allows a single color to be used. You can use a JTextPane instead
Two Swing classes support styled text: JEditorPane and its subclass JTextPane. The JEditorPane class is the foundation for Swing's styled text components and provides a mechanism through which you can add support for custom text formats.
If you want unstyled text, use a text area instead.
Source :Styling Dynamically
I afraid it is not possible to display colored text in jtextarea. you can only do that on jtextpane and here how .

Height of JLabel to grow with number of lines

I want my JLabel to be the height required for the particular string that is assigned to the text. The text string can can have any number of lines, which use the \n in the string. So if you have a string the "Hello\nit\nis\nme\n" the height of the label will automatically be adjusted to fit the 4 lines.
How to achieve that?
Use proper JTextComponent rather than use HTML formatted text in the JLabel
I think that for "Hello\nit\nis\nme\n" should be disabled JTextArea right JComponent
A JLabel1 would typically ignore the new-line character. To get multi-line text, either use a mutli-line component such as a JTextArea2 or use HTML formatting in the label.
Here is an example of using HTML formatting (screenshot above). The example uses styles (CSS) to cause the line breaks to be inserted automatically. This is much easier than calculating where to insert line-breaks in text blocks manually.
Of course, a JTextArea having methods like setLineWrap(true) & setWrapStyleWord(true) also handles line wrap automatically.

How to display bold text in only parts of JTextArea?

Can I alter the text of a JTextArea to bold (append text) and then back to normal and will it only display the bold text in bold and the rest as normal?
Also can the contents of JTextArea be saved as an RTF document?
No. What you're looking for is JEditorPane
This supports HTML (3.2?) which will allow you to use <font> (and other older tags) to provide rich text.
JEditorPane textarea = new JEditorPane("text/html", "");
textarea.setText("Here is some <b>bold text</b>");
EDIT: According to the javadoc I referenced above, JEditorPane also supports limited RTF. Don't forget to change the MIME to text/rtf
textArea.setFont(textArea.getFont().deriveFont(Font.BOLD, textArea.getFont().getSize()));
I believe you need a JTextPane or JEditorPane for that.

Categories