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?
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 .
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.
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 contains everything below it and the JTextPane is inside of everything above it.
I'm not sure why this works, but it does.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With