Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Revolving TextArea???

Reply
Thread Tools

Revolving TextArea???

 
 
SeanM
Guest
Posts: n/a
 
      03-09-2007
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

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      03-09-2007
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/
 
Reply With Quote
 
 
 
 
Steve Wampler
Guest
Posts: n/a
 
      03-09-2007
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 -- http://www.velocityreviews.com/forums/(E-Mail Removed)
The gods that smiled on your birth are now laughing out loud.
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      03-09-2007
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/
 
Reply With Quote
 
SeanM
Guest
Posts: n/a
 
      03-09-2007
On Mar 9, 10:18 am, Knute Johnson <(E-Mail Removed)>
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subclassing int for a revolving control number Chris Cioffi Python 13 07-20-2004 06:33 PM



Advertisments