Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Word wrap in JTextPane

Tags:

java

swing

I can't find an easy way to turn off word wrap in a JTextPane. I can't use JTextArea because I need different colors for different text. I have these big ugly lines that get uglier with word wrap turned on.

JTextArea has a setLineWrap() method, but I can't find it for JTextPane. Why?

like image 869
User1 Avatar asked Jan 05 '10 22:01

User1


People also ask

How do you make JTextPane wrap text?

To wrap the lines of JTextArea we need to call the setLineWrap(boolean wrap) method and pass a true boolean value as the parameter. The setWrapStyleWord(boolean word) method wrap the lines at word boundaries when we set it to true .

What is JTextPane?

A JTextPane is a subclass of JEditorPane. A JTextPane is used for a styled document with embedded images and components. A JTextPane is a text component that can be marked up with the attributes that are represented graphically and it can use a DefaultStyledDocument as the default model.


1 Answers

Okay, I found an easy solution. Put the JTextPane into the center of a JPanel with a Border layout. Then, put the JPanel into a JScrollPane.

So, the hierarchy looks like this:

  • JScrollPane
  • JPanel (w/ Border Layout)
  • JTextPane

JScrollPane contains everything below it and the JTextPane is inside of everything above it.

I'm not sure why this works, but it does.

like image 156
User1 Avatar answered Sep 21 '22 17:09

User1