Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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: 3262
30
  • 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: 3262
30
  • 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:


 
Yeah, but is it art? What do you think tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic