Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>Update Apr 2016</em></p> <p>This answer was created some years ago and the original answer is largely obsolete now. </p> <p>Since Java 8u40, Java has a <a href="https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TextFormatter.html" rel="noreferrer">TextFormatter</a> which is usually best for enforcing input of specific formats such as numerics on JavaFX TextFields:</p> <ul> <li><a href="https://stackoverflow.com/questions/31039449/java-8-u40-textformatter-javafx-to-restrict-user-input-only-for-decimal-number">Java 8 U40 TextFormatter (JavaFX) to restrict user input only for decimal number</a></li> <li><a href="https://stackoverflow.com/questions/35093145/string-with-numbers-and-letters-to-double-javafx/">String with numbers and letters to double javafx</a></li> </ul> <p>See also other answers to this question which specifically mention TextFormatter.</p> <hr> <p><em>Original Answer</em></p> <p>There are some examples of this in this <a href="https://gist.github.com/1962045" rel="noreferrer">gist</a>, I have duplicated one of the examples below:</p> <pre><code>// helper text field subclass which restricts text input to a given range of natural int numbers // and exposes the current numeric int value of the edit box as a value property. class IntField extends TextField { final private IntegerProperty value; final private int minValue; final private int maxValue; // expose an integer value property for the text field. public int getValue() { return value.getValue(); } public void setValue(int newValue) { value.setValue(newValue); } public IntegerProperty valueProperty() { return value; } IntField(int minValue, int maxValue, int initialValue) { if (minValue &gt; maxValue) throw new IllegalArgumentException( "IntField min value " + minValue + " greater than max value " + maxValue ); if (maxValue &lt; minValue) throw new IllegalArgumentException( "IntField max value " + minValue + " less than min value " + maxValue ); if (!((minValue &lt;= initialValue) &amp;&amp; (initialValue &lt;= maxValue))) throw new IllegalArgumentException( "IntField initialValue " + initialValue + " not between " + minValue + " and " + maxValue ); // initialize the field values. this.minValue = minValue; this.maxValue = maxValue; value = new SimpleIntegerProperty(initialValue); setText(initialValue + ""); final IntField intField = this; // make sure the value property is clamped to the required range // and update the field's text to be in sync with the value. value.addListener(new ChangeListener&lt;Number&gt;() { @Override public void changed(ObservableValue&lt;? extends Number&gt; observableValue, Number oldValue, Number newValue) { if (newValue == null) { intField.setText(""); } else { if (newValue.intValue() &lt; intField.minValue) { value.setValue(intField.minValue); return; } if (newValue.intValue() &gt; intField.maxValue) { value.setValue(intField.maxValue); return; } if (newValue.intValue() == 0 &amp;&amp; (textProperty().get() == null || "".equals(textProperty().get()))) { // no action required, text property is already blank, we don't need to set it to 0. } else { intField.setText(newValue.toString()); } } } }); // restrict key input to numerals. this.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler&lt;KeyEvent&gt;() { @Override public void handle(KeyEvent keyEvent) { if (!"0123456789".contains(keyEvent.getCharacter())) { keyEvent.consume(); } } }); // ensure any entered values lie inside the required range. this.textProperty().addListener(new ChangeListener&lt;String&gt;() { @Override public void changed(ObservableValue&lt;? extends String&gt; observableValue, String oldValue, String newValue) { if (newValue == null || "".equals(newValue)) { value.setValue(0); return; } final int intValue = Integer.parseInt(newValue); if (intField.minValue &gt; intValue || intValue &gt; intField.maxValue) { textProperty().setValue(oldValue); } value.set(Integer.parseInt(textProperty().get())); } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload