import java.util.Locale; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.ChoiceBox; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.control.PasswordField; import javafx.scene.control.Separator; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.Pane; import javafx.stage.Stage; import com.jyloo.syntheticafx.RootPane; import com.jyloo.syntheticafx.SyntheticaFXModena; import com.jyloo.syntheticafx.validation.Decorator; import com.jyloo.syntheticafx.validation.Validation; import com.jyloo.syntheticafx.validation.Validator; import com.jyloo.syntheticafx.validation.Validity; public class UserAccountDemo extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage stage) { new SyntheticaFXModena().init(); Parent content = createContent(); Scene scene = new Scene(new RootPane(stage, content)); stage.setTitle("Create New User Account Demo"); stage.setScene(scene); stage.show(); } public Parent createContent() { BorderPane content = new BorderPane(); content.setCenter(createUserAccountPane()); return content; } private Pane createUserAccountPane() { GridPane grid = new GridPane(); grid.setVgap(10); grid.setHgap(10); grid.setPadding(new Insets(5)); int row = 0; grid.add(new Label("Your Login Name: "), 0, row); TextField loginNameField = createTextField(); Validity v1 = Validation.installValidator(loginNameField, Validator.createEmptyValidator(loginNameField), Validation.Type.EAGER, Decorator.createWarningDecorator("Login name is mandatory!")); //custom validator - minimum name length is 6 //Validity v1 = Validation.installValidator(loginNameField, n -> {return n.getText().length() > 5;}, Validation.Type.LAZY, Decorator.createWarningDecorator("Minimum length is 6!")); grid.add(loginNameField, 1, row); row++; grid.add(new Label("Gender: "), 0, row); ChoiceBox choiceBox = createChoiceBox(); grid.add(choiceBox, 1, row); row++; grid.add(new Label("Email: "), 0, row); TextField eMailField = createTextField(); Validity v2 = Validation.installValidator(eMailField, n -> {return !n.getText().isEmpty() && n.getText().contains("@");}, Validation.Type.LAZY, Decorator.createWarningDecorator("Invalid Mail Address!")); grid.add(eMailField, 1, row); row++; grid.add(new Label("Password: "), 0, row); PasswordField passwordField = createPasswordField(); Validity v3 = Validation.installValidator(passwordField, n -> {return !n.getText().isEmpty() && n.getText().length() > 5;}, Validation.Type.LAZY, Decorator.createWarningDecorator("Use a minimum password length of 6.")); grid.add(passwordField, 1, row); row++; grid.add(new Label("Confirm Password: "), 0, row); PasswordField passwordFieldConfirmed = createPasswordField(); Validity v4 = Validation.installValidator(passwordFieldConfirmed, n -> {return n.getText().equals(passwordField.getText());}, Validation.Type.LAZY, Decorator.createWarningDecorator("Passwords do not match!")); grid.add(passwordFieldConfirmed, 1, row); row++; grid.add(new Label("Country: "), 0, row); ComboBox combo = createComboBox(); grid.add(combo, 1, row); row++; grid.add(new Label("Terms of use accepted: "), 0, row); CheckBox checkBox = createCheckBox(); Validity v5 = Validation.installValidator(checkBox, n -> {return n.isSelected();}, Validation.Type.EAGER, Decorator.createWarningDecorator("You have to accept terms of use!", Pos.BASELINE_RIGHT, 10, 0)); grid.add(checkBox, 1, row); row++; grid.add(new Separator(), 0, row, 2, 1); row++; grid.add(new Label(""), 0, row); final Button button = new Button("Create Account"); //button.disableProperty().bind(v1.and(v2).and(v3).and(v4).and(v5).not()); button.setOnAction(e -> { v1.revalidate(); v2.revalidate(); v3.revalidate(); v4.revalidate(); v5.revalidate(); boolean valid = v1.and(v2).and(v3).and(v4).and(v5).get(); System.out.println("User form is " + (valid ? "valid!" : "invalid!")); }); grid.add(button, 1, row, 1, 1); return grid; } private TextField createTextField() { TextField tf = new TextField(); tf.setPrefColumnCount(20); return tf; } private PasswordField createPasswordField() { return new PasswordField(); } private CheckBox createCheckBox() { CheckBox checkBox = new CheckBox(); return checkBox; } private ChoiceBox createChoiceBox() { ChoiceBox choiceBox = new ChoiceBox<>(); choiceBox.setItems(FXCollections.observableArrayList("Male", "Female")); return choiceBox; } private ComboBox createComboBox() { ComboBox combo = new ComboBox<>(); combo.setItems(FXCollections.observableArrayList(Locale.getISOCountries())); return combo; } }