JTextPane memory usage limit

I have an application that continuously receives data on a socket and then writes this data to a file while displaying this data in a JTextPane. Naturally, as data is written to the underlying JTextPane document, memory usage continues to increase.

Is there an easy way to limit the memory that JTextPane is allowed to use? I would like JTextPane to work just like a typical command line command line command works.

+7
java memory-management jtextpane
source share
2 answers

just check the contents and wipe it with the corresponding maximum buffer size .. since it is a JTextPane , you will work on the document class used by the text panel:

 void clampBuffer(int incomingDataSize) { Document doc = textPane.getStyledDocument(); int overLength = doc.getLength() + incomingDataSize - BUFFER_SIZE; if (overLength > 0) { doc.remove(0, over_length); } } 

This is just a fragment that I wrote, did not personally verify it. It is just to give you this idea. Of course, it must be started before adding text to the text panel.

Btw, if you are not using the features of the rich JTextPane editor, I suggest you use JTextArea , which is much more personal.

+7
source share

No, you need to count the characters when adding text and delete them if you think too much.

Note that the JTextPane has a DocumentModel under it, which can give you access to character numbers, as well as make deletions a bit more convenient.

0
source share

All Articles