Discussion:
JTextArea size limitiing question ...
(too old to reply)
Charles Morison
2006-03-24 04:43:29 UTC
Permalink
I'm using a JTextArea within a JScrollPane within a JViewPort. I am
using the JTextArea to mimic a HyperTerminal like terminal emulation
interface. When I set up the JTextArea I specify the viewable rows and
columns, but since it is inside the JScrollPane it obviously can grow
"infinitely" large (and eventually throw a heap memory exception). My
desire is to not allow this, but was hoping that there was something
within Java itself to passively limit the size of the JTextArea.

Currently I use it to receive diagnostics (through a serial
communications port) from an embedded device, so during testing I could
conceivably let this run for several days (which will most certainly
induce a heap memory exception). I am also logging all of the
diagnostics to a file so I have no need for the JTextArea to grow
"infinitely" large.

So is there a way to passively limit the ultimate size of a JTextArea
that I am missing or do I have to actively determine its size and trim
it down some how (and if so, any suggestion on how to delete "old" data
in a JTextArea)?

Thanks, in advance, for any help.
Rhino
2006-03-24 05:03:08 UTC
Permalink
I'm using a JTextArea within a JScrollPane within a JViewPort. I am using
the JTextArea to mimic a HyperTerminal like terminal emulation interface.
When I set up the JTextArea I specify the viewable rows and columns, but
since it is inside the JScrollPane it obviously can grow "infinitely"
large (and eventually throw a heap memory exception). My desire is to not
allow this, but was hoping that there was something within Java itself to
passively limit the size of the JTextArea.
Currently I use it to receive diagnostics (through a serial communications
port) from an embedded device, so during testing I could conceivably let
this run for several days (which will most certainly induce a heap memory
exception). I am also logging all of the diagnostics to a file so I have
no need for the JTextArea to grow "infinitely" large.
So is there a way to passively limit the ultimate size of a JTextArea that
I am missing or do I have to actively determine its size and trim it down
some how (and if so, any suggestion on how to delete "old" data in a
JTextArea)?
I'm not sure if there's a direct way to limit the capacity of a JTextArea.
I'd be a little surprised if there was and don't see anything like that in
the API. But maybe I just didn't look carefully enough.

However, a less direct approach seems very possible. One of the methods that
JTextArea inherits from JTextComponent is getText(); it returns a String
that has the contents of the JTextArea. Therefore, if your JTextArea is
called myTextArea, myTextArea.getText().length() will tell you how many
characters you have in your JTextArea.

You could use a Caret Listener to detect when the caret (cursor) for the
JTextArea indicates that the JTextArea is over a certain size; if that
happens, you can use the select() method of JTextComponent to find a desired
part of the JTextArea - the first 100 characters, for example - and then
remove those characters, presumably after writing them to somewhere
permanent if you still want the information in them. The easiest way to
remove them from the JTextArea would appear to be the cut() method of
JTextComponent.

You should be able to find all of the necessary techniques in the Java
Tutorial in the section about text areas or the section on caret listeners.

--
Rhino
Knute Johnson
2006-03-24 05:38:39 UTC
Permalink
Post by Charles Morison
I'm using a JTextArea within a JScrollPane within a JViewPort. I am
using the JTextArea to mimic a HyperTerminal like terminal emulation
interface. When I set up the JTextArea I specify the viewable rows and
columns, but since it is inside the JScrollPane it obviously can grow
"infinitely" large (and eventually throw a heap memory exception). My
desire is to not allow this, but was hoping that there was something
within Java itself to passively limit the size of the JTextArea.
Currently I use it to receive diagnostics (through a serial
communications port) from an embedded device, so during testing I could
conceivably let this run for several days (which will most certainly
induce a heap memory exception). I am also logging all of the
diagnostics to a file so I have no need for the JTextArea to grow
"infinitely" large.
So is there a way to passively limit the ultimate size of a JTextArea
that I am missing or do I have to actively determine its size and trim
it down some how (and if so, any suggestion on how to delete "old" data
in a JTextArea)?
Thanks, in advance, for any help.
Some of the JTextArea constructors take a Document argument. Use the
Document class below to set the size limit on your JTextArea.

//
//
// LengthLimitedDocument
//
//

import javax.swing.text.*;

public class LengthLimitedDocument extends PlainDocument {
protected int limit;

public LengthLimitedDocument(int limit) {
this.limit = limit;
}

public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
super.insertString(offs, str, a);
int length = getLength();
if (length > limit)
remove(0,limit/20); // remove 5% of document if over limit
}
}
--
Knute Johnson
email s/nospam/knute/
Charles Morison
2006-03-25 04:43:53 UTC
Permalink
I already have a document class for the text area since I am analyzing
characters typed/entered while the text area is in focus before they are
sent to the serial port and determining whether characters received from
the serial port should be shown in the text area or whether they are
part of an escape sequence (since I'm emulating a terminal). So this is
probably the best approach. Didn't really notice that the Document
class provided a size limit.

Thanks for the tip.
Post by Knute Johnson
Post by Charles Morison
I'm using a JTextArea within a JScrollPane within a JViewPort. I am
using the JTextArea to mimic a HyperTerminal like terminal emulation
interface. When I set up the JTextArea I specify the viewable rows
and columns, but since it is inside the JScrollPane it obviously can
grow "infinitely" large (and eventually throw a heap memory
exception). My desire is to not allow this, but was hoping that there
was something within Java itself to passively limit the size of the
JTextArea.
Currently I use it to receive diagnostics (through a serial
communications port) from an embedded device, so during testing I
could conceivably let this run for several days (which will most
certainly induce a heap memory exception). I am also logging all of
the diagnostics to a file so I have no need for the JTextArea to grow
"infinitely" large.
So is there a way to passively limit the ultimate size of a JTextArea
that I am missing or do I have to actively determine its size and trim
it down some how (and if so, any suggestion on how to delete "old"
data in a JTextArea)?
Thanks, in advance, for any help.
Some of the JTextArea constructors take a Document argument. Use the
Document class below to set the size limit on your JTextArea.
//
//
// LengthLimitedDocument
//
//
import javax.swing.text.*;
public class LengthLimitedDocument extends PlainDocument {
protected int limit;
public LengthLimitedDocument(int limit) {
this.limit = limit;
}
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
super.insertString(offs, str, a);
int length = getLength();
if (length > limit)
remove(0,limit/20); // remove 5% of document if over limit
}
}
Loading...