Using binding for visual help

While studying JavaFX classes, you can conveniently check how properties work using binding to a slider or checkbox—add them to your program, bind to the property you are interested in, and observe how different values of this property affect the corresponding node.

For example, let's look at this circle and bind a few sliders to its stroke and radius:

// chapter3/basics/CirclePropertiesDemo.java
public void start(Stage primaryStage) {
Circle circle = new Circle(150, 150, 40, Color.ANTIQUEWHITE);
circle.setStroke(Color.BLACK);

Slider sliderRadius = new Slider(0, 100, 40);
sliderRadius.relocate(80, 20);
sliderRadius.setShowTickLabels(true);
sliderRadius.setMajorTickUnit(20);

circle.radiusProperty()
.bind(sliderRadius.valueProperty());


Slider sliderStrokeWidth = new Slider(0, 10, 2);
sliderStrokeWidth.setShowTickLabels(true);
sliderStrokeWidth.setMajorTickUnit(2);
sliderStrokeWidth.relocate(80, 50);

circle.strokeWidthProperty()
.bind(sliderStrokeWidth.valueProperty());


Pane root = new Pane();
root.getChildren().addAll(sliderRadius, circle, sliderStrokeWidth);
primaryStage.setTitle("Hello Binding!");
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}

Here are the examples of this app with sliders moved to different positions:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.183.10