Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

latest 500 lines in a JtextArea inside a JScrollPane

436 views
Skip to first unread message

etantonio

unread,
Jul 22, 2009, 1:56:38 AM7/22/09
to
Good morning,
I've a JtextArea inside a JScrollPane,
I use an append to add lines to the JtextArea , my problem is that the
lines I add are very much so at the end this crash JVM, there's an
automatic way to limit the number of lines in the jtextarea in a way
that for example I've only latest 500 lines ?

Thanks,

Antonio
www.etantonio.it/en

John B. Matthews

unread,
Jul 22, 2009, 11:45:33 AM7/22/09
to
In article
<8d47970a-8bd6-45fb...@c1g2000yqi.googlegroups.com>,
etantonio <postm...@etantonio.it> wrote:

> I've a JtextArea inside a JScrollPane, I use an append to add lines
> to the JtextArea , my problem is that the lines I add are very much
> so at the end this crash JVM, there's an automatic way to limit the
> number of lines in the jtextarea in a way that for example I've only
> latest 500 lines ?

When the Document exceeds 500 lines, you could use replaceRange() to
clear lines at the Document's beginning each time you add text to the
Document's end. The several getLine*() methods seem apropos to this:

<http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Knute Johnson

unread,
Jul 22, 2009, 12:22:57 PM7/22/09
to

I use the following code all the time for a logging window so I don't
have overflow problems. When the limit is exceeded, some of the front
of the document is removed.

//
//
// LengthLimitedDocument
//
//

package com.knutejohnson.classes;

import javax.swing.text.*;

public class LengthLimitedDocument extends PlainDocument {
private 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/knute2009/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

Roedy Green

unread,
Jul 22, 2009, 12:52:41 PM7/22/09
to
On Tue, 21 Jul 2009 22:56:38 -0700 (PDT), etantonio
<postm...@etantonio.it> wrote, quoted or indirectly quoted someone
who said :

>I use an append to add lines to the JtextArea , my problem is that the
>lines I add are very much so at the end this crash JVM, there's an
>automatic way to limit the number of lines in the jtextarea in a way
>that for example I've only latest 500 lines ?

you can programmatically scroll the JScrollPane. See
http://mindprod.com/jgloss/jscrollpage.html

You could also remove early text from the JTextArea as you go.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"The industrial civilisation is based on the consumption of energy resources that are inherently limited in quantity, and that are about to become scarce. When they do, competition for what remains will trigger dramatic economic and geopolitical events; in the end, it may be impossible for even a single nation to sustain industrialism as we have know it in the twentieth century."
~ Richard Heinberg, The Party�s Over: Oil, War, and the Fate of Industrial Societies

0 new messages