Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

too much text in textpane

Alex Wolf
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Im writing a messenger, and I'm experiencing a problem with too much text in JTextPanes. That means, the more messages I receive, the slower the messenger reacts because there's so much text in the JTextPane. This goes to the point of it being unusable. I usually fix this by closing the window and reopening it so that there's no text to display. This works for a while, but its annoying.

I want fix this by setting the JTextPane to display up to a certain number of lines, for example 200, and everything thats exceeds this, should be written to a file so that I can recall it if the user chooses to scroll to the top.
When receiving messages, the text is formatted and then added to the JTextPane and when recalling it should be formatted the same way.

I need some help getting started. I don't know how to extract the formatted text from the JTextPane.
Its easy to set text with html formatting:

but retrieving the text just gives me plain text.
Jared Cope
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Basically, you will need to become familar with 3 main objects:
1. The textPane
2. The editorKit for the textPane
3. The document object that sits behind the textPane

There are a few good tutorials around that walk you through it all. The learning curve is a bit steep, but there really is no other way. All the interesting stuff happens in the document object and the editorKit that the textPane uses. It sounds like you will need to extend the editorKit class and over-ride some of the insert methods to get your '200 lines' function working.

To speed ahead, you should learn about how the document is made up of a number of elements. Elements have their special properties such as formatting and colouring (markup in HTML terms). Your best bet is to traverse the document object and get all the elements then have a peak at their properties. You will probably learn a lot from that.

Jumping ahead, if you want to see the markup behind a textpane, try calling the dump method of the Document. Just use System.out as the argument to dump to your console.

Hope this has helped you.

Cheers, Jared.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!