By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,179 Members | 2,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,179 IT Pros & Developers. It's quick & easy.

Restrict Value in TextField using KeyEvents

jkmyoung
Expert 100+
P: 2,057
I was trying to create an applet with a TextField that would only accept an integer, and ignore any other keystrokes. Eg, if a user typed in an 'f' into the field, the TextField should ignore it, and not even put the f into the textbox.

However, the TextField does not appear to update until one keypress after I need it to. Eg. if I typed, 10fg, it would end up as "10f" then after the g, become "g10".

I'm currently using the keyPressed Event
The TextField is named delayTime
The stored integer is named delayInt

Expand|Select|Wrap|Line Numbers
  1. public void keyPressed(KeyEvent keyEvent) {
  2.  
  3.     String delayStr = delayTime.getText();
  4.     char keyChar = keyEvent.getKeyChar();
  5.     //System.out.println("Key pressed:"+keyChar);
  6.     try{
  7.         if (delayStr.equals("")){
  8.             delayInt = 0;
  9.         } else {
  10.             int i;
  11.             i = Integer.parseInt(delayStr);
  12.             delayInt = i;
  13.         }
  14.     }catch(Exception e){    
  15.         delayTime.setText((new Integer(delayInt)).toString());
  16.     }
  17. }
  18.  
Is there a better way to restrict the text in a textfield? My current code does not appear to be doing it. A different event perhaps?

Link used http://java.sun.com/docs/books/tutor...ylistener.html
Feb 25 '08 #1
Share this Question
Share on Google+
2 Replies


BigDaddyLH
Expert 100+
P: 1,216
My rule of thumb is to never use Keylistener -- there's always a better solution, like JFormattedTextField: http://java.sun.com/docs/books/tutor...textfield.html
Feb 25 '08 #2

100+
P: 539
I was trying to create an applet with a TextField that would only accept an integer, and ignore any other keystrokes. Eg, if a user typed in an 'f' into the field, the TextField should ignore it, and not even put the f into the textbox.

However, the TextField does not appear to update until one keypress after I need it to. Eg. if I typed, 10fg, it would end up as "10f" then after the g, become "g10".

I'm currently using the keyPressed Event
The TextField is named delayTime
The stored integer is named delayInt

Expand|Select|Wrap|Line Numbers
  1. public void keyPressed(KeyEvent keyEvent) {
  2.  
  3.     String delayStr = delayTime.getText();
  4.     char keyChar = keyEvent.getKeyChar();
  5.     //System.out.println("Key pressed:"+keyChar);
  6.     try{
  7.         if (delayStr.equals("")){
  8.             delayInt = 0;
  9.         } else {
  10.             int i;
  11.             i = Integer.parseInt(delayStr);
  12.             delayInt = i;
  13.         }
  14.     }catch(Exception e){    
  15.         delayTime.setText((new Integer(delayInt)).toString());
  16.     }
  17. }
  18.  
Is there a better way to restrict the text in a textfield? My current code does not appear to be doing it. A different event perhaps?

Link used http://java.sun.com/docs/books/tutor...ylistener.html
The difference between keypressed and keyreleased is that, the keyevent triggered by keypressed is then first to execute before the character appear at the text field... And the keyevent in keyreleased is then be executed after the character is printed to the textfield,

You can not really see the character printed if you use the keypressed when you entered it and following the said event...(if you want to erase it)

Another way to ignore any non-integer,

Algo...
(Trigger this in keyreleased event)...
Every input that the user do, copy first the old text (Before the user input again or at the start)inside the textfield in a temp variable,
Test if the last character in the new text is not a non-integer...

If the last character is non-integer, then simply retrieve the value you have stored from the tempvariable

if satisfied, then allow...



For me, it is safe to implement this, how about your new idea?


Concerned,
Sukatoa(Shadow shaman)
Feb 25 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.