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 / General / March 2007

Tip: Looking for answers? Try searching our database.

Revolving TextArea???

Thread view: 
SeanM - 09 Mar 2007 16:40 GMT
I have done several searches but the problem is I don't even know what
to call what it is I want to do.  Forgive me if this is super simple,
I have only been doing Java for about a month.

I have written a client/server application where the client sends
something to the server and the server sends an acknowledgment.  I
only show the last thing sent and received.  I want to have a history
button though that will open up a JTextArea and show the history.  So
far so good but here is where my problem is.  I only want the
JTextArea to show a limited size.  Once the limit has been reached I
still want to be able to add to it but when I do I wan the stuff at
the beginning to get removed.

So my questions are:
What is something like this called, I know it has a name but for the
life of me I can not remember what it is...for lack of a better name I
have been calling it a revolving text area.
What is the best way to implement such a thing?  Does Java have
something built in for this?

Again sorry if this is simple stuff.

Thanks for your time.

SM
Knute Johnson - 09 Mar 2007 17:07 GMT
> I have done several searches but the problem is I don't even know what
> to call what it is I want to do.  Forgive me if this is super simple,
[quoted text clipped - 21 lines]
>
> SM

I do that all the time.  You need to add a Document to your JTextArea.

//
//
//  FixedLengthDocument
//
//

package com.knutejohnson.classes;

import javax.swing.text.*;

public class FixedLengthDocument extends PlainDocument {
    private int len;

    public FixedLengthDocument(int len) {
        this.len = len;
    }

    public void setDocumentLength(int len) {
        this.len = len;
    }

    public int getDocumentLength() {
        return len;
    }

    public void insertString(int offs, String str, AttributeSet a)
     throws BadLocationException {
        if (str == null)
            return;
        int strLen = str.length();
        int textLen = getLength();
        if (strLen + textLen <= len)
            super.insertString(offs, str, a);
    }
}

Signature

Knute Johnson
email s/nospam/knute/

Steve Wampler - 09 Mar 2007 17:44 GMT
>> ....Once the limit has been reached I
>> still want to be able to add to it but when I do I wan the stuff at
>> the beginning to get removed.
> I do that all the time.  You need to add a Document to your JTextArea.
...
>     public void insertString(int offs, String str, AttributeSet a)
>      throws BadLocationException {
[quoted text clipped - 5 lines]
>             super.insertString(offs, str, a);
>     }

Hmmm, doesn't this discard the new lines instead of the old ones (no
'revolving')?  Still, it shouldn't be hard to modify insertString
to remove lines at the beginning.  The trick is to remove enough
entire *lines* (not just characters) to free up space for the incoming
string.  Still not too hard of a modification, 'tho.

Signature

Steve Wampler -- swampler@noao.edu
The gods that smiled on your birth are now laughing out loud.

Knute Johnson - 09 Mar 2007 18:18 GMT
>>> ....Once the limit has been reached I
>>> still want to be able to add to it but when I do I wan the stuff at
[quoted text clipped - 16 lines]
> entire *lines* (not just characters) to free up space for the incoming
> string.  Still not too hard of a modification, 'tho.

Yes is does.  I hope it isn't old timers that getting to me.

Kindly disregard my last post and look at this one.

//
//
//  LengthLimitedDocument
//
//

package com.knutejohnson.classes;

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/

SeanM - 09 Mar 2007 20:28 GMT
On Mar 9, 10:18 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> >>> ....Once the limit has been reached I
> >>> still want to be able to add to it but when I do I wan the stuff at
[quoted text clipped - 52 lines]
> Knute Johnson
> email s/nospam/knute/

Thanks, I will give this a try.

SM


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.