限制JTextPane内存使用量 [英] Limit JTextPane memory usage

查看:119
本文介绍了限制JTextPane内存使用量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它不断地在套接字上接收数据,然后将这些数据记录到文件中,同时在JTextPane中显示这些数据。当然,当数据写入JTextPane的底层文档时,内存使用量会继续增加。

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

是否有一种简单的方法来限制允许使用JTextPane的内存?我希望JTextPane的工作方式类似于典型命令shell的命令历史记录的工作方式。

Is there a simple way of limiting the memory which the JTextPane is allowed use? I would like the JTextPane to work similar to how a typical command shell's command history works.

推荐答案

只需检查内容并擦除它相应的最大缓冲区大小..因为它是一个 JTextPane 你将使用textpane使用的文档类:

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);
   }
}

这只是我写的一个片段,没有亲自检查..它只是给你这个想法。当然应该在向textPane添加文本之前运行。

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.

顺便说一句,如果你没有使用 JTextPane的丰富编辑器功能我建议你使用更加轻松的 JTextArea

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

这篇关于限制JTextPane内存使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆