Revolving TextArea???

S

SeanM

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
 
K

Knute Johnson

SeanM said:
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);
}
}
 
S

Steve Wampler

Knute said:
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.
 
K

Knute Johnson

Steve said:
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
}
}
 
S

SeanM

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
}

}

Thanks, I will give this a try.

SM
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top