๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #9355

Re: How can I limit a JTextPane length ?

#9355
From: John P. McGrath
Date: Tue, 18 Apr 2000 00:00
33 lines
1625 bytes
On Mon, 17 Apr 2000 23:01:12 +0200, Jean-Fran๏ฟฝois Leroux wrote:

> I have a jTextPane and would like to limit the legth of the
> possible entry (say 200 characters). I search the Swing Api
> for that but couldn't manage and find it.  I tried the
> KeyPressed event with 'JtextPane1.getText().lentgh' but it
> was unsuccessfull. 

You might try something like this:

    textPane.getDocument().addDocumentListener( new DocumentListener() {
        void changedUpdate( DocumentEvent event ) {}
        void removeUpdate( DocumentEvent event ) {}
        void insertUpdate( DocumentEvent event ) {
            Document doc = event.getDocument();
            int len = doc.getLength();
            if ( len > 200 ) {
                try {
                    doc.remove( 200, len - 200 );
                } catch ( BadLocationException ex ) {
                    //  Not likely to happen.
                }
            }
        }
    } );

Note:  I have not tried to compile or run this, so it could have an 
error.  Debugging is left as an exercise for the programmer.

-- 
John P. McGrath
<JPMcGrath@fast.net>
<jpm@nscs.fast.net>
Technical Details
Message-ID: <MPG.1365a23a1575dd52989885@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <8dftfj$ps89@bornews.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 111d45,aa99ae09612c00a7
X-Google-Attributes: gid111d45,public
From: John P. McGrath <JPMcGrath@fast.net>
Subject: Re: How can I limit a JTextPane length ?
Date: 2000/04/18
Message-ID: <MPG.1365a23a1575dd52989885@forums.borland.com>
X-Deja-AN: 612548243
References: <8dftfj$ps89@bornews.borland.com>
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.swing