org.datafx.samples.validation.property.PropertyValidationController Maven / Gradle / Ivy
package org.datafx.samples.validation.property;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import org.datafx.controller.FXMLController;
import org.datafx.controller.validation.ValidatorFX;
import org.datafx.controller.validation.context.Validator;
import org.datafx.controller.validation.event.ValidationFinishedEvent;
import org.datafx.controller.validation.event.ValidationFinishedHandler;
import javax.annotation.PostConstruct;
import javax.validation.ConstraintViolation;
import javax.validation.constraints.NotNull;
@FXMLController("view.fxml")
public class PropertyValidationController {
@FXML
private Button validateButton;
@FXML
private TextField nameField;
@FXML
private TextArea descriptionField;
@NotNull
private StringProperty name = new SimpleStringProperty();
@NotNull
private StringProperty description = new SimpleStringProperty();
@Validator
private ValidatorFX validator;
@PostConstruct
public void init() {
nameField.textProperty().bindBidirectional(name);
descriptionField.textProperty().bindBidirectional(description);
validator.setOnValidationFinished(new ValidationFinishedHandler() {
@Override
public void handle(ValidationFinishedEvent event) {
for (ConstraintViolation violation : event.getViolations()) {
System.out.println(violation.getMessage());
}
}
});
validateButton.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent arg0) {
validator.validateAllProperties();
}
});
}
}