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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>