472,794 Members | 3,648 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 software developers and data experts.

Restrict Value in TextField using KeyEvents

jkmyoung
2,057 Expert 2GB
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
2 5363
BigDaddyLH
1,216 Expert 1GB
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
sukatoa
539 512MB
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

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

Similar topics

0
by: Ed Severn | last post by:
I have a problem. My JTable has some rows that are editable via DefaultCellEditor(new JTextField()). I have attached a KeyListener to that JTextField object. When I type into the field, no...
1
by: LRW | last post by:
Below is some of the code I'm using. I have a PHP page generating a list of items. I've made it so that each one has a radiobutton with a unique value. You can click on the radiobutton and it will...
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
2
by: atif | last post by:
I hav a prob with the textfield i want to restrict this field so that only integer values should b entered e.g 0-9 n no english or special characters should b entered in the text field Posted...
0
by: Gidi | last post by:
Hi, i have a dataGrid, and i allocated a keyevents for one of the cells (i did some manipulation on this cell), and i have a strange problem: all the keyevents work except the one i really need...
10
by: sconeek | last post by:
hi all, i have a textfield where i would like the user to input only Y or N. can somebody tell me how can i restrict the user from entering any other character, number or special character....
1
by: macintoshhondo | last post by:
Hi ! i am a newbie and dont know javascript much. what i really need is a simple javascript code that can insert number in the value section of the different forms from the one form. FORM 1:...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.