5

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
naresh
  • 10,332
  • 25
  • 81
  • 124
  • See the [validation approach](http://www.jidesoft.com/blog/2013/06/11/highlights-of-the-jidefx-beta-release-3-of-3/) of the [JideFX library](https://github.com/jidesoft/jidefx-oss). – jewelsea Sep 16 '13 at 15:57

3 Answers3

3

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/ (!Wayback)

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.

yellowsir
  • 741
  • 1
  • 9
  • 27
zenbeni
  • 7,019
  • 3
  • 29
  • 60
1

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

kervin
  • 11,672
  • 5
  • 42
  • 59
0

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.

Community
  • 1
  • 1
kithril
  • 1,183
  • 2
  • 12
  • 22