Article View: borland.public.jbuilder.java.language
Article #14644Re: JTextField size
From: Tom.Eugelink
Date: Tue, 10 Aug 1999 00:00
Date: Tue, 10 Aug 1999 00:00
58 lines
2222 bytes
2222 bytes
The problem we found with this approach is that it is not possible to
check contents when the user exists the textfield. For example a date
or a number.
On Mon, 09 Aug 1999 10:05:07 -0600, David <david.v.clark@lmco.com>
wrote:
>Here is the solution that I am using to restrict the number of characters
>the JTextField allows. I found it on the java web site and changed the beep
>since it would beep for each JTextfield at the constructor. Anyway, it
>seems to do the trick. Hope it helps.
>
>import java.awt.Toolkit;
>import javax.swing.*;
>import javax.swing.text.*;
>
>// An extension of PlainDocument that restricts the length of the content it
>contains
>public class FixedLengthPlainDocument extends PlainDocument {
> private int maxLength;
>
> public FixedLengthPlainDocument(int maxLength) {
> this.maxLength = maxLength;
> }
>
> public void insertString(int offset, String str, AttributeSet a) throws
>BadLocationException {
> if (getLength() + str.length() > maxLength) {
>
> str = str.substring(0, str.length() - (getLength() +
>str.length() - maxLength));
> super.insertString(offset, str, a);
> //Toolkit.getDefaultToolkit().beep();
> }
> else {
> super.insertString(offset, str,a);
> }
> }
>}
>
>
>
>David wrote:
>
>> Good day -
>>
>> Using JBuilder 3 -
>>
>> I have several JTextFields that the user types into. I would like to
>> restrict the number of characters to 10 (for example). Has anyone done
>> this? I thought perhaps DocumentListener was the solution, but couldn't
>> get it to work.
>>
>> I would greatly appreciate any help you can offer.
>>
>> David
>
Technical Details
Message-ID:
<37afd81b.192927@forums.inprise.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<37A9FA85.279DCA47@lmco.com> <37AEFC33.A3E71EB2@lmco.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,4e0efe8fe5a13afc X-Google-Attributes: gidf74b3,public From: Tom.Eugelink@OVSoftware.com Subject: Re: JTextField size Date: 1999/08/10 Message-ID: <37afd81b.192927@forums.inprise.com> X-Deja-AN: 510966270 References: <37A9FA85.279DCA47@lmco.com> <37AEFC33.A3E71EB2@lmco.com> Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.language