Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Keystroke validation in JTextField

Reply
Thread Tools

Re: Keystroke validation in JTextField

 
 
Lew
Guest
Posts: n/a
 
      10-01-2007
icogs wrote:
>> Simple one I hope:


Please do not multipost.

It causes answers to appear in only one of the multiple groups to which you
posted your message. You need to keep the answers together, e.g., by
cross-posting, or better, by sticking with only one group for your post.

>> How can I intercept keystrokes to a JTextField so I could, for
>> example, implement a digit-only text field [question mark omitted in orig.]


Knute Johnson's answer for the other group who missed it:
> You don't want to do it that way with JComponents. Use a Document to
> control those sorts of things. Look at PlainDocument in the docs and
> see an example below of how to implement a document that only allows
> upper case letters.
>
> //
> //
> // UpperCaseDocument
> //
> //
>
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
> import javax.swing.text.*;
>
> public class UpperCaseDocument extends PlainDocument {
> int length = 0;
>
> public UpperCaseDocument() {
> }
>
> public UpperCaseDocument(int length) {
> this.length = length;
> }
>
> public void insertString(int offs, String str, AttributeSet a)
> throws BadLocationException {
> if (str == null) {
> return;
> }
> if (length > 0)
> if (str.length() + getLength() > length)
> str = str.substring(0,length - getLength());
> char[] upper = str.toCharArray();
> for (int i = 0; i < upper.length; i++)
> upper[i] = Character.toUpperCase(upper[i]);
> super.insertString(offs, new String(upper), a);
> }
> }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Keystroke validation in JTextField icogs Java 0 10-01-2007 07:18 PM
Keystroke saving w/ IEEE.Numeric_Std JustJohn VHDL 6 03-29-2006 05:21 PM
Keystroke Count Herb Stull ASP .Net 4 10-22-2003 07:33 PM
KeyStroke madness =?ISO-8859-1?Q?St=E9phane_St-Pierre?= Java 1 09-19-2003 11:17 PM
Which keystroke do you press for the OR logical operator? Fred Java 6 08-02-2003 11:34 AM



Advertisments