I need to insert a number field into my UI. So I need to check the key events on a text field in order to check whether the input character is a number. I created a class by extending TextField. If there is a method in TextField class which handles keyEvents, i can simply overide that method with suits to number field. Any ideas?
Thanks
There is a tip on FXExperience that deals with an issue just like that. To paraphrase, you extend the
TextField
and override thereplaceText()
andreplaceSelection()
methods, filtering all input that is not a number.Once implemented, both methods should follow this template:
Here is the CustomText field I have written. It handles both Numbers only input and also maximumSize. Its a custom control that can be used in FXML and also the properties can be set in FXMl itself.
}
Inherit from TextField and override replaceText as such, to obtain a Double values only TextField:
Update 27 May 2016
Java 8u40 introduced the TextFormatter class which is the recommended way to accomplish this functionality (though the solution provided in this answer will still work). For further information see Uwe's answer, Hassan's answer and other answers mentioning TextFormatter to the following question:
There is also this solution from another answer to this question which I haven't tried, but looks good and a StackOverflow moderator deleted:
The code above misses the UnaryOperator filter for the TextFormatter, which you usually also require (otherwise, the field won't display restrict user input to only the formatted value, it will just allow you to monitor the unformatted value via the text formatters value property). To extend the solution to use a filter, code like that below could be used:
When running the above example, edit the input field and press the enter key to see the value updated (updated value is output to
System.out
when changed).For a tutorial see:
This is the same solution which Urs references, however I just placed it in a fully executable program to provide an example in context and modified the regular expression (by adding
*
on the end) so that copy and paste works and it does not have the issue Uluk refers to. The solution seems pretty simple and will likely suffice for most purposes:Alternate Solutions
You might also be interested in my alternate solution in JavaFX example of binding a slider value to a editable TextField. In that solution I extend TextField to expose an IntegerProperty on the field for simple binding purposes. The alternate solution is similar to that outlined by the original poster in their updated question (i.e. an event filter is added to restrict input data from key events), but additionally a ChangeListener is added on the TextField's text property to ensure that copy and pasted values are only accepted if they are numeric.
There are some other solutions to this question in the JavaFX Forum thread Numeric Textfield in JavaFX 2.0? which includes a reference to the number fields from FXExperience controls.
Found a solution. :)
For a number of TextFiled (include decimal point)