JTextArea size limitiing question ...  
Author Message
Charles Morison





PostPosted: 2006-3-24 12:43:00 Top

java-programmer, JTextArea size limitiing question ... 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.
 
Charles Morison





PostPosted: 2006-3-24 12:46:00 Top

java-programmer >> JTextArea size limitiing question ... 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





PostPosted: 2006-3-24 13:03:00 Top

java-programmer >> JTextArea size limitiing question ...
"Charles Morison" <email***@***.com> wrote in message
news:RNKUf.6287$email***@***.com...
> 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.


 
 
Knute Johnson





PostPosted: 2006-3-24 13:39:00 Top

java-programmer >> JTextArea size limitiing question ... Charles Morison wrote:
> 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/
 
 
IchBin





PostPosted: 2006-3-24 15:54:00 Top

java-programmer >> JTextArea size limitiing question ... Charles Morison wrote:
> 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.

I would do what the IBM's mainframe VM operating system did, and
probably still does, for CP and CMS internal trace tables. Just round
robin them. That is, after n-number, you define, just start to overwrite
it again from row 0. This would assumes that you had a time stamp on the
row for later review.

--

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
 
Charles Morison





PostPosted: 2006-3-25 12:44:00 Top

java-programmer >> JTextArea size limitiing question ... 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.

Knute Johnson wrote:
> Charles Morison wrote:
>> 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
> }
> }
>