For Programmers: Free Programming Magazines  


Home > Archive > Java Help > March 2006 > JTextArea size limitiing question ...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author JTextArea size limitiing question ...
Charles Morison

2006-03-24, 4:14 am

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, 4:14 am


"Charles Morison" <morison@mindspring.com> wrote in message
news:RNKUf.6287$x94.2001@newsread1.news.pas.earthlink.net...
> 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, 4:14 am

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/
Charles Morison

2006-03-25, 4:02 am

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:
>
> 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
> }
> }
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com