Java – limit jtextpane memory usage
I have an application that continuously receives data on the socket, records the data to a file, and displays the data in jtextpane Of course, memory usage continues to increase as data is written to the underlying documents of jtextpane
Is there a simple way to limit the memory allowed to use jtextpane? I want jtextpane to work like the command history of a typical command shell
Solution
Just check the content and erase it to the maximum buffer size accordingly Because it is a jtextpane, you will use the document class used by textpane:
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 clip I wrote. I didn't check it myself... Just to give you this idea Of course, you should run it before adding text to textpane
By the way, if you don't use the rich editor features of jtextpane, I suggest you use a more concise jtextarea