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)
Update Apr 2016
This answer was created some years ago and the original answer is largely obsolete now.
Since Java 8u40, Java has a TextFormatter which is usually best for enforcing input of specific formats such as numerics on JavaFX TextFields:
See also other answers to this question which specifically mention TextFormatter.
Original Answer
There are some examples of this in this gist, I have duplicated one of the examples below:
The
if
clause is important to handle inputs like 0.5d or 0.7f which are correctly parsed by Int.parseInt(), but shouldn't appear in the text field.This code works fine for me even if you try to copy/paste.
this Code Make your textField Accept only Number
In recent updates of JavaFX, you have to set new text in Platform.runLater method just like this:
It's a good idea to set caret position too.
I don't like exceptions thus I used the
matches
function from String-Class