1

Im trying to to check whenever a user types a character into textbox if it is a number. If it is not it should immediately remove it from the textbox.

What happens is I type in the number 1 (or any number or character), and it removes the value from the textbox when it is obviously a number.

Here is the event I am using:

private void txtLengthAKeyReleased(java.awt.event.KeyEvent evt) {
    removeLastChar(txtLengthA); //pass the textbox 
}

Here is removeLastChar() method:

public static void removeLastChar(JTextField txt)
{
    //Get string from text field
    String str = txt.getText();
    //Make sure length > 0
    if( (str.length()) != 0)
    {
        //Get the last char of the string
        String s = str.substring(str.length()-1, str.length()-1);
        System.out.println(s);   //test debug
        //If not numeric (try/catch Double.parseDouble)
        if(!isNumeric(s));
        {
            //Remove last char from the text box
            str = str.substring(0, str.length()-1);
            txt.setText(str);
        }
    }
}

Check if string is numeric:

isNumeric() function:
    public static boolean isNumeric(String str)  
    {  
      try  
      {  
        double d = Double.parseDouble(str);  
      }  
      catch(NumberFormatException nfe)  
      {  
        return false;  
      }  
      return true;  
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Kairan
  • 5,342
  • 27
  • 65
  • 104
  • Please have a look at this [thread](http://stackoverflow.com/q/9477354/1057230), answer to this question and the reason why it is closed can give you a good idea about how to go around this situation :-) **KeyEvents** are too low level for Swing, instead use **DocumentFilter** as described in some answers and that link. – nIcE cOw Oct 22 '12 at 11:00
  • Ahh I was afraid you guys would say that. – Kairan Oct 24 '12 at 01:39

3 Answers3

1

Using KeyListeners to filter or modify JTextComponets is only going to end in tears.

You should be using a DocumentFilter

Check out Limit the Characters in the text field using document listner, Deleting last keystroke in JTextField if invalid and JTextField limiting character amount input and accepting numeric only for examples (especially the links from the answer in the last question)

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

What will happen if user types a non-numeric character in middle of the text field? Or in beginning?

You can implement a better solution using DocumentFilter: http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html#filter

You just need to call super.insert... when you are sure the string being inserted or replaced is valid. (Numeric in your case).

Ashwinee K Jha
  • 9,187
  • 2
  • 25
  • 19
0

It should be

        String s = str.substring(str.length()-1, str.length();

With your code the value of s is always an empty string.

To test if a single character is a digit, use

Character.isDigit(str.substring(0,1))
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190