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
[code=java]
public void keyPressed(KeyE vent keyEvent) {
String delayStr = delayTime.getTe xt();
char keyChar = keyEvent.getKey Char();
//System.out.prin tln("Key pressed:"+keyCh ar);
try{
if (delayStr.equal s("")){
delayInt = 0;
} else {
int i;
i = Integer.parseIn t(delayStr);
delayInt = i;
}
}catch(Exceptio n e){
delayTime.setTe xt((new Integer(delayIn t)).toString()) ;
}
}
[/code]
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
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
[code=java]
public void keyPressed(KeyE vent keyEvent) {
String delayStr = delayTime.getTe xt();
char keyChar = keyEvent.getKey Char();
//System.out.prin tln("Key pressed:"+keyCh ar);
try{
if (delayStr.equal s("")){
delayInt = 0;
} else {
int i;
i = Integer.parseIn t(delayStr);
delayInt = i;
}
}catch(Exceptio n e){
delayTime.setTe xt((new Integer(delayIn t)).toString()) ;
}
}
[/code]
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
Comment