πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: JTextField Question

#10457
From: Johan Forsberg
Date: Thu, 30 Sep 1999 00:00
30 lines
1565 bytes
Michel PοΏ½rez <mperez@eurobanco.com> skrev i
diskussionsgruppsmeddelandet:7sofk2$g0818@forums.borland.com...
> I have just tried this way, but I'm not too clear.
>

Try this:

public class MyField extends JTextField {
    int maxLength=10; // You should probably have get/set for this
    protected Document createDefaultModel() {
        return new MyDocument();
    }
    class MyDocument extends PlainDocument {
        public void insertString(int offs, String str, AttributeSet a)
               throws BadLocationException {
            if (maxlength>0) {
                int len=getText().length(), len2=str.length();
                if (len+len2>maxlength) {
                    len2 = len2+maxlength-len2-len;
                    if (len2<=0)
                        return;
                    // Chop!
                    str=str.substring(0, len2);
                }
            }
            super.insertString(offs, str, a);
        }
    }
}
Technical Details
Message-ID: <7t01gj$ji611@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <7snok1$a1n4@forums.borland.com> <7sofk2$g0818@forums.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 111d45,8f9acf093401c6af
X-Google-Attributes: gid111d45,public
From: "Johan Forsberg" <jfo@SPAM_SPAM_EGGS_SPAMstmf.se>
Subject: Re: JTextField Question
Date: 1999/09/30
Message-ID: <7t01gj$ji611@forums.borland.com>
X-Deja-AN: 531172981
References: <7snok1$a1n4@forums.borland.com> <7sofk2$g0818@forums.borland.com>
X-Priority: 3
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
Organization: Another Netscape Collabra Server User
X-MSMail-Priority: Normal
Newsgroups: borland.public.jbuilder.java.swing