tags:

views:

87

answers:

1

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?

+1  A: 

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.

User1
I think the reason this works is because when the text pane is in a panel with BorderLayout (as center) which is in a ScrollPane, its preferred size grows to whatever the contents require (i.e. borderlayout gives it as much space as it wants and since it's in a scroll pane it can grow as much as it wants. When it was just in a panel or frame directly, its size was constrained by the size of the panel/frame. Once text length was > available length, the JTextPane started wrapping words to fit them in the available space (which was fixed).
Joshua McKinnon
Well said! (I have to put more words or SO won't let me post this comment)
User1