Skip to main content
summaryrefslogtreecommitdiffstats
blob: c3c80047aa0562f9c9f849a4890004c9b44a6406 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package org.eclipse.fx.ui.controls.sample;

import org.eclipse.fx.core.Status;
import org.eclipse.fx.core.Status.State;
import org.eclipse.fx.ui.controls.form.DefaultForm;
import org.eclipse.fx.ui.controls.form.StatusNode;
import org.eclipse.fx.ui.panes.GridData;
import org.eclipse.fx.ui.panes.GridData.Alignment;
import org.eclipse.fx.ui.panes.GridLayoutPane;
import org.eclipse.fx.ui.controls.form.Form.BindingConfiguration;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class ValidationSample extends Application {

	static class VM {
		private StringProperty firstname = new SimpleStringProperty(this, "firstname");
		private StringProperty lastname = new SimpleStringProperty(this, "lastname");

		public VM(String firstname, String lastname) {
			this.firstname.set(firstname);
			this.lastname.set(lastname);
		}
	}

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		GridLayoutPane p = new GridLayoutPane();
		p.setNumColumns(2);

		DefaultForm form = new DefaultForm();
		Label title = new Label("My Form");
		title.setGraphic(new StatusNode(form.validationStatusProperty()));
		title.setStyle("-fx-font-size: 2em; -fx-font-weight: bold;");

		GridLayoutPane.setConstraint(title, new GridData(Alignment.FILL,Alignment.CENTER,true,false,2,1));
		p.getChildren().add(title);

		VM vm = new VM("Tom", "Schindl");

		{
			Label l = new Label("Vorname");
			p.getChildren().add(l);

			TextField field = new TextField();
			p.getChildren().add(form.builder("firstname",field.textProperty(), vm.firstname, BindingConfiguration.VALIDATE_TO_OPPOSITE)
				.decoratedNode(field)
				.decoratedNodeMutator(new GridData(GridData.FILL_HORIZONTAL), GridLayoutPane::setConstraint)
				.validator( s -> s.isEmpty() ? Status.status(State.ERROR, -1, "Vorname muß befüllt sein", null) : Status.ok())
				.build()
				.nodeWithStatus());
		}

		{
			Label l = new Label("Nachname");
			p.getChildren().add(l);

			TextField field = new TextField();
			p.getChildren().add(form.builder("lastname",field.textProperty(), vm.lastname, BindingConfiguration.VALIDATE_TO_OPPOSITE)
				.decoratedNode(field)
				.decoratedNodeMutator(new GridData(GridData.FILL_HORIZONTAL), GridLayoutPane::setConstraint)
				.validator( s -> s.isEmpty() ? Status.status(State.ERROR, -1, "Nachname muß befüllt sein", null) : Status.ok())
				.build()
				.nodeWithStatus());
		}

		primaryStage.setScene(new Scene(p,800,600));
		primaryStage.show();
	}

}

Back to the top