Revolving TextArea???

Discussion in 'Java' started by SeanM, Mar 9, 2007.

  1. SeanM

    SeanM Guest

    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
    SeanM, Mar 9, 2007
    #1
    1. Advertising

  2. SeanM wrote:
    > 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
    >


    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);
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Mar 9, 2007
    #2
    1. Advertising

  3. Knute Johnson wrote:
    > SeanM 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
    >> 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 {
    > if (str == null)
    > return;
    > int strLen = str.length();
    > int textLen = getLength();
    > if (strLen + textLen <= len)
    > 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.

    --
    Steve Wampler --
    The gods that smiled on your birth are now laughing out loud.
    Steve Wampler, Mar 9, 2007
    #3
  4. Steve Wampler wrote:
    > Knute Johnson wrote:
    >> SeanM 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
    >>> 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 {
    >> if (str == null)
    >> return;
    >> int strLen = str.length();
    >> int textLen = getLength();
    >> if (strLen + textLen <= len)
    >> 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.
    >


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

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Mar 9, 2007
    #4
  5. SeanM

    SeanM Guest

    On Mar 9, 10:18 am, Knute Johnson <>
    wrote:
    > Steve Wampler wrote:
    > > Knute Johnson wrote:
    > >> SeanM 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
    > >>> 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 {
    > >> if (str == null)
    > >> return;
    > >> int strLen = str.length();
    > >> int textLen = getLength();
    > >> if (strLen + textLen <= len)
    > >> 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.

    >
    > 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
    > }
    >
    > }
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute/


    Thanks, I will give this a try.

    SM
    SeanM, Mar 9, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bei

    msword-like textarea?

    Bei, Jun 25, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,231
    mike C.
    Jul 2, 2003
  2. Chris Cioffi
    Replies:
    13
    Views:
    488
    Christopher T King
    Jul 20, 2004
  3. Augustus
    Replies:
    1
    Views:
    340
    Ken Schaefer
    Sep 10, 2003
  4. Textarea Inside of a textarea

    , Feb 4, 2006, in forum: ASP General
    Replies:
    6
    Views:
    318
    Anthony Jones
    Feb 5, 2006
  5. Replies:
    1
    Views:
    298
    Bart Van der Donck
    Jul 4, 2007
Loading...

Share This Page