问题
I am newbie to javaFX. I created one registration form using fxml. Now i want to implement the field validation functionality. I am trying to implement the validation for TextField but still I am not getting it. Please can anyone help me.
thanks Naresh
回答1:
Unfortunately, there is no validation framework within JavaFX. Even frameworks such as Granite Data Services had troubles with bean validation with JavaFX: http://granitedataservices.com/blog/2012/11/29/thoughts-about-javafx-and-bean-validation/
If you are interested with bean validation with JavaFX, Granite generate java beans with JavaFX Property fields with bean validation enabled (you validate your java bean which is binded to your javafx components). It can be a good solution, or a good inspiration for your problem.
回答2:
As @zenbeni pointed out, there is not automated validation, but you can implement your own using event handlers. How you want to implement it will determine which event handler you choose to implement. They can very wildly in complexity. Here is someone else's attempt to create a fully validate Text Field component: JavaFX 2.2 FXML Validated TextField
You can get away with a simpler implementation, by using the setOnAction Handler described here http://docs.oracle.com/javafx/2/api/javafx/scene/control/TextField.html#setOnAction(javafx.event.EventHandler), but if you're going to be doing this many times, you'll want something more complete like the implementation above.
回答3:
My suggestion is to use JideFX for the field validations and other special controls like Calendar and popup windows.
Please check www.jidesoft.com
回答4:
You can validate on lost focus on the control. That is a pretty common cross-platform method...
textField.focusedProperty().addListener((observable, oldValue, newValue) ->
{
if(textField.isFocused() == false)
{
LOGGER.debug("Validate on lost focus here...");
}
});
JavaFX 8
来源:https://stackoverflow.com/questions/18825436/javafx-field-validation