This question already has an answer here:
I know that this question must have been asked and answered a million times, but I just can't find an easy solution. I have a JTextField that is meant to accept only positive integers as input. I need a way to make sure that nothing else gets input here.
I already have a keyListener attached to this control. Removing the other code that this listener is there to handle, I have this:
txtAnswer.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
/* Restrict input to only integers */
if (key < 96 && key > 105) e.setKeyChar('');
};
});
As you can see, I'm trying to use the the KeyCode to check whether the key just pressed falls within the range of integers. This seems to work. But what I want to do is to simply disregard the entry if it falls outside of this range. The code e.setKeyChar('')
was meant to handle this, but it doesn't work. The code will compile, but it has no visible effect.
Can anybody tell me if I am on the right track? What can I replace e.setKeyChar('')
with to make this work? Or am I totally going in the wrong direction?
Thanks.
You can also use
JFormattedTextField
, which is much simpler to use. Example:Here's one approach that uses a keylistener,but uses the keyChar (instead of the keyCode):
http://edenti.deis.unibo.it/utils/Java-tips/Validating%20numerical%20input%20in%20a%20JTextField.txt
Another approach (which personally I find almost as over-complicated as Swing's JTree model) is to use Formatted Text Fields:
http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html
When you type integer numbers to JtextField1 after key release it will go to inside try , for any other character it will throw NumberFormatException. If you set empty string to jTextField1 inside the catch so the user cannot type any other keys except positive numbers because JTextField1 will be cleared for each bad attempt.
Do not use a KeyListener for this as you'll miss much including pasting of text. Also a KeyListener is a very low-level construct and as such, should be avoided in Swing applications.
The solution has been described many times on SO: Use a DocumentFilter. There are several examples of this on this site, some written by me.
For example: using-documentfilter-filterbypass
Also for tutorial help, please look at: Implementing a DocumentFilter.
Edit
For instance:
Why is this important?
I can't believe I haven't found this simple solution anywhere on stack overflow yet, it is by far the most useful. Changing the Document or DocumentFilter does not work for JFormattedTextField. Peter Tseng's answer comes very close.
I used to use the Key Listener for this but I failed big time with that approach. Best approach as recommended already is to use a DocumentFilter. Below is a utility method I created for building textfields with only number input. Just beware that it'll also take single '.' character as well since it's usable for decimal input.