Friday, August 30, 2013

JavaFX example: bind more than one value with NumberBinding

This example show to to bind the value of sliderSum to the sum of slider1 and slider2 values with NumberBinding.

bind more than one value with NumberBinding
Bind more than one value with NumberBinding

package javafx_numberbinding;

import javafx.application.Application;
import javafx.beans.binding.NumberBinding;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_NumberBinding extends Application {
    
    @Override
    public void start(Stage primaryStage) {

        Slider slider1 = new Slider(0, 100, 0);
        Slider slider2 = new Slider(0, 50, 0);
        Slider sliderBind1 = new Slider(0, 100, 0);
        Slider sliderBind2 = new Slider(0, 50, 0);
        Slider sliderSum = new Slider(0, 150, 0);
        
        sliderBind1.valueProperty().bind(slider1.valueProperty());
        sliderBind2.valueProperty().bind(slider2.valueProperty());
        
        NumberBinding numberSum = slider1.valueProperty().add(slider2.valueProperty());
        sliderSum.valueProperty().bind(numberSum);
        
        VBox vBox = new VBox();
        vBox.setPadding(new Insets(5, 5, 5, 5));
        vBox.setSpacing(5);
        vBox.getChildren().addAll(
                slider1, 
                slider2,
                sliderBind1,
                sliderBind2,
                sliderSum);
        
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}


No comments:

Post a Comment