hi, Im just a newbie in java, I really want to learn but sometimes I lost my guts to learn coz Im always stuck on something, for example in JTextField UI, I want the UI to accept just a numeric input and prompt the user when the input is not a numeric type and also it will not display on the UI. Can somebody give a hint on how to flush the unnumeric key and I will do the rest, Id tried the InputVerifier class but still it accept and display the unnumeric key...Thanks

Recommended Answers

All 8 Replies

This depends on how the user is indicating that they are done filling out the JTextField. Are they clicking a button? Is an event fired when the key is pressed? Since you didn't say I'll assume the user is clicking a button once they put their input in the text field. Then implement ActionListener for that button, and in the actionPerformed method, get the text field's text. Then use Double.parseDouble(textFieldsTextHere); -- if it does not throw an Exception, then the entry was a number. There might be a better way but that is pretty easy to do and will probably fit your needs.

This should help you out:
http://www.devx.com/tips/Tip/14510

Just add a pop-up message when the if evaluates to false, and you're good to go!

Good luck

And how does that longer code that needs to be adapted offer improvement over my answer? Besides which, that code seems like it will keep going regardless of any errors. So it would take 123ddd45 and produce the String 12345, would it not? Maybe I read it incorrectly. Not trying to start an argument here, just asking.

I don't know if it is better than yours, I posted it as an alternative. This code gets the keypress directly after the user presses any key, so all the non-digit chars are ignored.
Yes, it will take the string 123ddd45 and produce 12345, but this would usually not occur since all key presses generates a string with only one character, however if you copy and paste it wouldn't look very good :)

Implement KeyListener interface and show your work.

thanks guys for tips that u have given, u know id reinstalled my netbeans, hehehe...

@BestJew: i alridi have a code that u want to imply and yah its good to use but im just a little bit perfectionist that all i want is to check everytime the user presses a key not just only by pressing a button, and i do have also a keylistener...

@di2daer: the link gives a hint on how to solve my problem...

guys how about if ill get the event.getKeyChar() and test it if its a number or not, if it not delete that char and update the UI.

private class abc extends KeyAdapter() {
     public void keyReleased(KeyEvent event) {
              JTextField textField = (JTextField) event.getComponent();
              if (textField.getText().isEmpty()) 
                    return;                
              char c = event.getKeyChar();
              if (!Character.isDigit(c)) {
                      StringBuffer str = new StringBuffer(textField.getText());
                      int caretPos = source.getCaretPosition();
                      str.deleteCharAt(caretPos - 1);
                      //send the data back to the source
                      source.setText(str.toString());
              }
     }
}

this code run well for me, but i am eager for some approach

Wrap up your source code with BB Code tags.
What do you say?

private class abc extends KeyAdapter() {
     public void keyReleased(KeyEvent event) {
         JTextField textField = (JTextField) event.getComponent();
        if (textField.getText().isEmpty()) 
             return; 
         char c = event.getKeyChar();
        if (!Character.isDigit(c)) {
             StringBuffer str = new StringBuffer(textField.getText());
               int caretPos = source.getCaretPosition();
               str.deleteCharAt(caretPos - 1);
          //send the data back to the source
              source.setText(str.toString());
       }
   }
}

ah yah! sori about that...yup thats the code, it runs well at first but i pressed some keys like the navigation keys it gives me an error. by the way thanks adatapost...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.