Article View: borland.public.jbuilder.java.swing
Article #9792Re: Limiting the number of characters for a JTextField
From: David M. Karr
Date: Fri, 28 Jan 2000 00:00
Date: Fri, 28 Jan 2000 00:00
27 lines
1878 bytes
1878 bytes
>>>>> "Jeff" == Jeff Marendo <marendoj@commos.com> writes:
Jeff> Hello, I'm trying to write a customized class for JTextField where
Jeff> you can set a maximum amount of characters that can be entered for
Jeff> it. I think I'm almost there, and my code is posted below. I have a
Jeff> couple of things that I'm stuck on, though.
Jeff> One, I'd like it so that if the user hits the left arrow key (or
Jeff> right) the local variable called 'localLimit' isn't incremented.
Jeff> Currently, it's incrementing.
Jeff> Second, I'd like it so that if the cursor is after the last char in
Jeff> the text field, the 'localLimit' will not be decreased if one hits
Jeff> the 'Delete' key.
Jeff> Third, does anyone know why the keyboard is trying to do something
Jeff> with my functionality for the 'Enter' key other than moving the focus
Jeff> to the next component?
Jeff> Please let me know. Thanks.
Sigh. This is the wrong way to do this. It is much easier to implement a new
subclass of the PlainDocument class, such that it overrides the
"insertString()" and "remove()" methods. In those methods, you keep track of
the current number of characters in the "model" so you can restrict the total
input length. This will also deal properly with pasting into the field, which
your code doesn't deal with at all.
Technical Details
Message-ID:
<uemb2dtug.fsf@attws.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<86q8sn$rje4@bornews.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,a26b3ba2769008e3 X-Google-Attributes: gid111d45,public From: david.karr@attws.com (David M. Karr) Subject: Re: Limiting the number of characters for a JTextField Date: 2000/01/28 Message-ID: <uemb2dtug.fsf@attws.com> X-Deja-AN: 578812637 Sender: DKarr@WA02513 References: <86q8sn$rje4@bornews.borland.com> Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.swing