🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: JTextField not obeying columns restriction?

#12984
From: Vladimir Olenin
Date: Thu, 15 Oct 1998 00:00
65 lines
2713 bytes
This is a multi-part message in MIME format.
--------------42D9C3E7630D436B3FFC41A9
Content-Type: text/plain; charset=koi8-r
Content-Transfer-Encoding: 7bit

Again, message to everyone: read Swing tutorial of javasoft! It can
answer to most of the questions!

So, coming back to the question, the solution is following:
====================
 .....
static int NUMBER_OF_CHARS = 1;

JTextField jtf = new JTextField();
jtf.setDocument(new LimitedCharsDocument())
....

class LimitedCharsDocument extends DefaultStyledDocument{
  public void insertString(int offs, String str, AttributeSet a)
                         throws BadLocationException {
    if ((getLength() + str.length()) <= NUMBER_OF_CHARS)
       super.insertString(offs, str, a);
    else
       Toolkit.getDefaultToolkit().beep();
    }
}
========================
If u want to get extended info about JTextComponent, just email me, and
i'll send u some pages from that tutorial.

                                       Vladimir

Adam Chace wrote:
> 
> I have a JTextField component in which I'd like to restrict the number
> of characters that can be added to, say, 10. If I set the columns
> property to 10 or any number, the user can still enter as many
> characters as they want. This is even true of the AWT Field Control. Am
> I missing something?

-- 
>-------<===========>-------<
Join in Java community now!
http://www.geocities.com/SiliconValley/Heights/9815
(NB: the page is under construction now)
>-------<===========>-------<
--------------42D9C3E7630D436B3FFC41A9
Content-Type: text/x-vcard; charset=koi8-r;
 name="keenonjava.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Vladimir Olenin
Content-Disposition: attachment;
 filename="keenonjava.vcf"

begin:vcard
n:Vladimir;Olenin
x-mozilla-html:FALSE
adr:version:2.1
version:2.1
email;internet:keenonjava@collegemail.com
note:Java programmer. Open for project development offers. See my home page at: http://www.geocities.com/SiliconValley/Heights/9815/
end:vcard


--------------42D9C3E7630D436B3FFC41A9--
Technical Details
Message-ID: <3625327D.AF8168AB@collegemail.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <3624289E.5FE9A56E@apogeeis.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,19d0a032f428dc48
X-Google-Attributes: gid111d45,public
From: Vladimir Olenin <keenonjava@collegemail.com>
Subject: Re: JTextField not obeying columns restriction?
Date: 1998/10/15
Message-ID: <3625327D.AF8168AB@collegemail.com>
X-Deja-AN: 401140179
References: <3624289E.5FE9A56E@apogeeis.com>
X-Accept-Language: en,ru
Content-Type: multipart/mixed; boundary="------------42D9C3E7630D436B3FFC41A9"
Organization: Another Netscape Collabra Server User
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.swing