Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Turn off text wrapping in JTextPane  RSS feed

 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to use different font colours, I need to use a JTextPane rather than a JTextArea.

So I've changed the JTextArea in my application to a JTextPane, and find that there is word wrapping where I didn't have any before. I want to turn all wrapping off and just use the scrollbars on the JScrollPane it's sitting on. How do I do this?

I found something here that includes a demo of about 150 lines of code that I don't really understand.

Maybe there's another way, getting the pane to grow automatically so that the text doesn't have to wrap, or something like that?

Here's a screenshot of my app (put together with the help of NetBeans' GUI builder). The text output is taken from another class as a single string with \n characters in it.


And here's what happens when I increase the length of the lines in the text to 160 columns:
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Wrap Text Pane
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The solution is to add the text pane to a JPanel using a BorderLayout and then add the panel to the scroll pane


Great, that worked, thanks.

Is there a way to stop the scroll pane changing position to the far bottom-right whenever I update the display?

I could just check the positions before every update using ScrollPane.getScrollPosition() and reset them after, but it would be better if I could stop them changing.
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Text Area Scrolling show how to turn on automatic scrolling for a text area. I would guess if you use the proper "update policy" you should be able to turn off scrolling for a text pane, but I've never tested it.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, that worked too!

Updated the background panel's constructor with this, where "graph" is the JTextPane:


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!