New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some JavaFX samples for JBV #24

Closed
wants to merge 2 commits into
base: master
from
Jump to file or symbol
Failed to load files and symbols.
+758 −0
Diff settings

Always

Just for now

Next

JavaFX Samples

  • Loading branch information...
hendrikebbers committed May 11, 2017
commit 79b0235a4ce47d785df4f683361ede0985a113d0
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.hibernate.demos</groupId>
<artifactId>javafx-validation-exsample</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,9 @@
/**
*
* This package contains several JavaFX related samples of JBV. The "sample1" sample is
* quite small and only use some basic functionality. Each sample adds some additional
* features.
*
* @author Hendrik Ebbers
*/
package com.canoo.validation;
@@ -0,0 +1,45 @@
package com.canoo.validation.sample1;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
/**
*
* @author Hendrik Ebbers
*/
public class Model {
@NotEmpty
private StringProperty name = new SimpleStringProperty();
private Property<@Min(50) Integer> count = new SimpleObjectProperty<>();
public String getName() {
return name.get();
}
public StringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
public Integer getCount() {
return count.getValue();
}
public Property<Integer> countProperty() {
return count;
}
public void setCount(Integer count) {
this.count.setValue(count);
}
}
@@ -0,0 +1,24 @@
package com.canoo.validation.sample1;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author Hendrik Ebbers
*/
public class Sample extends Application {
public void start(final Stage primaryStage) throws Exception {
final FXMLLoader loader = new FXMLLoader(Sample.class.getResource("view.fxml"));
loader.setController(new ViewController());
primaryStage.setScene(new Scene(loader.load()));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
@@ -0,0 +1,55 @@
package com.canoo.validation.sample1;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Set;
/**
*
* @author Hendrik Ebbers
*/
public class ViewController implements Initializable {
@FXML
private TextField nameField;
@FXML
private Slider countSlider;
@FXML
private Button validateButton;
@FXML
private Label validateMessageLabel;
public void initialize(final URL location, final ResourceBundle resources) {
final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
final Model model = new Model();
final Validator validator = factory.getValidator();
nameField.textProperty().bindBidirectional(model.nameProperty());
model.countProperty().bind(Bindings.createObjectBinding(() ->
countSlider.valueProperty().intValue()
, countSlider.valueProperty()));
validateButton.setOnAction(e -> {
final Set<ConstraintViolation<Model>> violations = validator.validate(model);
validateMessageLabel.setText(violations.stream().map(v -> v.getMessage()).reduce("", (a, b) -> a + b));
});
}
}
@@ -0,0 +1,45 @@
package com.canoo.validation.sample2;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
/**
*
* @author Hendrik Ebbers
*/
public class Model {
@NotEmpty
private StringProperty name = new SimpleStringProperty();
private Property<@Min(50) Integer> count = new SimpleObjectProperty<>();
public String getName() {
return name.get();
}
public StringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
public Integer getCount() {
return count.getValue();
}
public Property<Integer> countProperty() {
return count;
}
public void setCount(Integer count) {
this.count.setValue(count);
}
}
@@ -0,0 +1,24 @@
package com.canoo.validation.sample2;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author Hendrik Ebbers
*/
public class Sample extends Application {
public void start(final Stage primaryStage) throws Exception {
final FXMLLoader loader = new FXMLLoader(Sample.class.getResource("view.fxml"));
loader.setController(new ViewController());
primaryStage.setScene(new Scene(loader.load()));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
@@ -0,0 +1,51 @@
package com.canoo.validation.sample2;
import com.canoo.validation.sample1.Model;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Set;
/**
*
* @author Hendrik Ebbers
*/
public class ViewController implements Initializable {
@FXML
private TextField nameField;
@FXML
private Slider countSlider;
@FXML
private Label validateMessageLabel;
public void initialize(final URL location, final ResourceBundle resources) {
final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
final Model model = new Model();
final Validator validator = factory.getValidator();
nameField.textProperty().bindBidirectional(model.nameProperty());
model.countProperty().bind(Bindings.createObjectBinding(() ->
countSlider.valueProperty().intValue()
, countSlider.valueProperty()));
validateMessageLabel.textProperty().bind(Bindings.createStringBinding(() -> {
final Set<ConstraintViolation<Model>> violations = validator.validate(model);
return violations.stream().map(v -> v.getMessage()).reduce("", (a, b) -> a + System.lineSeparator() + b);
}, model.nameProperty(), model.countProperty()));
}
}
@@ -0,0 +1,45 @@
package com.canoo.validation.sample3;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
/**
*
* @author Hendrik Ebbers
*/
public class Model {
@NotEmpty
private StringProperty name = new SimpleStringProperty();
private Property<@Min(50) Integer> count = new SimpleObjectProperty<>();
public String getName() {
return name.get();
}
public StringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
public Integer getCount() {
return count.getValue();
}
public Property<Integer> countProperty() {
return count;
}
public void setCount(Integer count) {
this.count.setValue(count);
}
}
@@ -0,0 +1,24 @@
package com.canoo.validation.sample3;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author Hendrik Ebbers
*/
public class Sample extends Application {
public void start(final Stage primaryStage) throws Exception {
final FXMLLoader loader = new FXMLLoader(Sample.class.getResource("view.fxml"));
loader.setController(new ViewController());
primaryStage.setScene(new Scene(loader.load()));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.