Article View: borland.public.jbuilder.java.swing
Article #11682Re: JdbTextField
From: Jason Burton
Date: Mon, 19 Apr 1999 00:00
Date: Mon, 19 Apr 1999 00:00
71 lines
2584 bytes
2584 bytes
Walter Jacobs <walter.jacobs@skynet.be> wrote in message
news:7f9h1n$n8n2@forums.borland.com...
> Hi,
>
> I am looking for a way to prevent the user to input more characters than
are
> allowed in the linked queryDataSet Column .
> I am using JBuilder 2.0
>
> For example
> "Name" may have a maximum of 6 characters
>
> I set:
> jdbTextField.setAutoscrolls(false);
> jdbTextField.setColumns (6);
>
> and in the queryDataSet:
> column1.setDisplayMask("aaaaaA")
>
> Still I can enter as much as characters I want in the TextField. Even
> horizontal scrolling still does happen.
>
> When I enter 10 characters, there is no error given and the first 6
> characters are put in the variable (column).
>
> Has someone a solution and an explanation why both setAutoscroll and
> setColumn does not work?
>
>
> thanks for the help
>
> Walter
>
>
> I am using JBuilder 2.0
Walter,
I use a modified JTextField that uses a custom Document that extends
PlainDocument. I add a maxlength variable to the document (and to the text
field, so that I can say myTextField.setMaxLength(6)). Anyway, override
the insertString method of the PlainDocument to handle the maximum length.
Example:
public void insertString(int offs, String str, AttributeSet a) throws
BadLocationException
{
String insString;
if (upshifted) // I also use an upshifted flag to indicate text
must be in all uppercase
insString = str.toUpperCase();
else
insString = str;
if (maxLength > -1 && getLength() + str.length() > maxLength) // if
maxlength is set and
// text length plus inserted text length is greater than maxlength
{
insString = insString.substring(0, maxLength - getLength()); //
trim off excess text
Toolkit.getDefaultToolkit().beep(); // beep to say the text is
too long
}
super.insertString(offs, insString, a);
}
Hope this helps.
Jason Burton
Technical Details
Message-ID:
<7ffdk7$sf78@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<7f9h1n$n8n2@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,3f6990afaf35f86b X-Google-Attributes: gid111d45,public From: "Jason Burton" <Jason.Burton@McLeodSoftware.com> Subject: Re: JdbTextField Date: 1999/04/19 Message-ID: <7ffdk7$sf78@forums.borland.com> X-Deja-AN: 468305271 References: <7f9h1n$n8n2@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