How can I disable input of any symbol except digits to JTextField
?
Option 1) change your JTextField with a JFormattedTextField, like this:
try {
MaskFormatter mascara = new MaskFormatter("##.##");
JFormattedTextField textField = new JFormattedTextField(mascara);
textField.setValue(new Float("12.34"));
} catch (Exception e) {
...
}
Option 2) capture user's input from keyboard, like this:
JTextField textField = new JTextField(10);
textField.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if ( ((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)) {
e.consume(); // ignore event
}
}
});