I need to restrict input into a TextField to integers. Any advice?
相关问题
- I get an exception when trying to swap elements in
- JFX scale image up and down to parent
- Dragging an undecorated Stage in JavaFX
- Jasper: error opening input stream from url
- is it normal for image inputs to be omitted from t
相关文章
- 放在input的text下文本一直出现一个/(即使还没输入任何值)是什么情况
- Show a different value from an input that what wil
- Is there a way to hide the new HTML5 spinbox contr
- Programmatically interrupting raw_input
- Low quality icon in taskbar of a Stage. JavaFX
- Bootstrap input field inside tooltip popover remov
- Loading custom font using JavaFX 8 and css
- ValueError: too many values to unpack (expected 2)
Mmmm. I ran into that problem weeks ago. As the API doesn't provide a control to achieve that,
you may want to use your own one. I used something like:
It's used the same way that a normal
TextBox
,but has also a
value
attribute which stores the entered integer.When the control looses the focus, it validates the value and reverts it (if isn't valid).
Very old thread, but this seems neater and strips out non-numeric characters if pasted.
This method lets TextField to finish all processing (copy/paste/undo safe). Does not require to extend classes and allows you to decide what to do with new text after every change (to push it to logic, or turn back to previous value, or even to modify it).
For your case just add this logic inside. Works perfectly.
Here is a simple class that handles some basic validations on
TextField
, usingTextFormatter
introduced in JavaFX 8u40EDIT:
(Code added regarding Floern's comment)
You can use it like this:
or you can instantiate it in a fxml file, and apply it to a customTextField with the according properties.
app.fxml:
CustomTextField.class:
Code on github
Try this simple code it will do the job.
If you want to apply the same listener to more than one TextField here is the simplest solution: