473,511 Members | 15,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Restrict Value in TextField using KeyEvents

jkmyoung
2,057 Recognized Expert Top Contributor
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 5459
BigDaddyLH
1,216 Recognized Expert Top Contributor
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 Contributor
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
3130
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
16606
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
9585
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
1334
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
1021
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
8874
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
1920
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:...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.