Restrict Value in TextField using KeyEvents

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    Restrict Value in TextField using KeyEvents

    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
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    My rule of thumb is to never use Keylistener -- there's always a better solution, like JFormattedTextF ield: http://java.sun.com/docs/books/tutor...textfield.html

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      Originally posted by jkmyoung
      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
      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)
      Last edited by sukatoa; Feb 25 '08, 11:47 PM. Reason: changing

      Comment

      Working...