• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Turn off text wrapping in JTextPane

 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No Wrap Text Pane
 
Luigi Plinge
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great, that worked too!

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


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic