Share the post "How To Create A Numbers Only TextField In Java FX 2"
User Narayan posted this helpful code for a TextField control in Java FX 2 to accept only numeric values.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
tf.lengthProperty().addListener(new ChangeListener<Number>(){ @Override public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) { if (newValue.intValue() > oldValue.intValue()) { char ch = tf.getText().charAt(oldValue.intValue()); // Check if the new character is the number or other's if (!(ch >= '0' && ch <= '9' )) { // if it's not number then just setText to previous one tf.setText(tf.getText().substring(0,tf.getText().length()-1)); } } } }); |