Limit JTextPane memory usage

无人久伴 提交于 2019-12-05 05:36:13

just check the content and wipe it accordingly to a maximum buffer size.. since it's a JTextPane you would work on 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 snippet I wrote, didn't check it personally.. it's just to give you the idea. Of course it should be run before adding text to textPane.

Btw if you are not using the rich editor capabilities of the JTextPane I suggest you to use a JTextArea that is much ligher.

Nope, you have to count characters as you add text, and delete some when you think it's too much.

Note that JTextPane has a DocumentModel underneath it which can give you access to character counts and also make deletions a little handier maybe.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!