Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

JTextArea size limitiing question ...

Thread view: 
Charles Morison - 24 Mar 2006 05:43 GMT
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 - 24 Mar 2006 06:03 GMT
> I'm using a JTextArea within a JScrollPane within a JViewPort.  I am using
> the JTextArea to mimic a HyperTerminal like terminal emulation interface.
[quoted text clipped - 14 lines]
> 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 - 24 Mar 2006 06:38 GMT
> I'm using a JTextArea within a JScrollPane within a JViewPort.  I am
> using the JTextArea to mimic a HyperTerminal like terminal emulation
[quoted text clipped - 17 lines]
>
> 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
    }
}

Signature

Knute Johnson
email s/nospam/knute/

Charles Morison - 25 Mar 2006 05:43 GMT
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.

>> I'm using a JTextArea within a JScrollPane within a JViewPort.  I am
>> using the JTextArea to mimic a HyperTerminal like terminal emulation
[quoted text clipped - 45 lines]
>     }
> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.