3 Replies Latest reply: Feb 24, 2007 4:04 PM by camickr RSS

    Validating a JText field for characters

    556517
      Hi i have a jTextfield i am expecting the user to enter a value between 1-9 only with the following code:
      public class KeyCatcher extends KeyAdapter
          {
               
               public void keyPressed(KeyEvent evt)
               {
                //int temp = evt.getKeyCode();
                 
                 String stringValue = textField.getText();
                 
                 int value = Integer.parseInt(stringValue);
                 
                 if( m_nShapesToDraw == 0 )
                  {               
                     if(( value >=1 ) && ( value <= 9 ) ) 
                         {
                          m_nShapesToDraw = value;
                               hasEnteredValue = true;     
                               cPanel.repaint() ;                     
                                
                         }
                   } 
               }     
           
          }
      As you can see i am converting the string into an int and comparing it against 1-9. Now i want to add another validation to check if it is a character. Can somone explain to me how i would do that in my text field.
      Please not: disregard the line of code:
      if( m_nShapesToDraw == 0 )
      as this used for somthing else. Thanks