JTextField

0 views
Skip to first unread message

JJames

unread,
Oct 13, 2000, 3:00:00 AM10/13/00
to
Hello

I'm using jdk1.3.
I've just created 4 text boxes only to realize I can't control the amount of
information that's placed in those text boxes.

Question: What method do I call in order to set the maximum length within a
text box so that users are limited to what can put in there?

Thank you in advance

Ray Tayek

unread,
Oct 15, 2000, 3:00:00 AM10/15/00
to
In article <39e73f8c$1...@athena.netset.com>, jja...@infosage.com says...
> ... just created 4 text boxes only to realize I can't control the amount of

> information that's placed in those text boxes.
>
> Question: What method do I call in order to set the maximum length within a
> text box so that users are limited to what can put in there?

look at the validated text field on suns site or in core java 2 by
horstmann and cornell. basicaly you watch each character that the user
insert or deletes, so you can control the length of the string that way
and perform other validation tests on stuff the luser types.

hth
--
Ray (will hack java for food) http://home.earthlink.net/~rtayek/
orange county java users group http://www.ocjug.org/
want privacy? http://www.freedom.net/
hate spam? http://samspade.org/ssw/

Mathieu Peltier

unread,
Oct 16, 2000, 4:08:19 AM10/16/00
to
JJames a écrit :

>
> Hello
>
> I'm using jdk1.3.
> I've just created 4 text boxes only to realize I can't control the amount of

> information that's placed in those text boxes.
>
> Question: What method do I call in order to set the maximum length within a
> text box so that users are limited to what can put in there?
>
> Thank you in advance


You can try this:

public class LimitedDocument extends PlainDocument {

private int limit;

LimitedDocument(int limit) {
this.limit=limit;
}

public void insertString(int offset, String s, AttributeSet
attributeSet)
throws BadLocationException {

int end = getEndPosition().getOffset() - 1;
int n=limit-end;

if ( n > 0 ) {

if ( s.length() > n )
s=s.substring(0,n-1);

super.insertString(offset,s,attributeSet);
}
}
}

public class LimitedField extends JTextField {

public LimitedField(int maxLength) {
super(maxLength);
setDocument(new LimitedDocument(maxLength));
}
}

or see here:
http://www.javaworld.com/javaqa/2000-05/02-qa-0512-textbox.html

--
Mathieu Peltier

Reply all
Reply to author
Forward
0 new messages