JavaFX: Button jumps off the binding with SplitPane.Divider

668 views Asked by At

I have SplitPane with two AnchorPane (left and right side). There are Label, TextField and Button on each AnchorPane, they are arranged in one line. Label must bind to the left side AnchorPane, Button to the right and TextField must be stretched between them. Left AnchorPane must be bind to left part of SplitPane.

My code works, but when I move divider after some time Buttons jump off the binding with SplitPane. AnchorPane width bind to SplitPane.Divider, Label, TextField, Button bind to AnchorPane. Can you help me? Sorry for my English.

    import javafx.application.Application;
    import javafx.beans.binding.DoubleBinding;
    import javafx.beans.value.ObservableValue;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.SplitPane;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.AnchorPane;
    import javafx.stage.Stage;

    public class Main extends Application {

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

        @Override
        public void start(Stage stage) {
            stage.setTitle("BackUpManager");
            AnchorPane root = new AnchorPane();
            Scene scene = new Scene(root, 800,600);

            SplitPane splitPane = new SplitPane();
            splitPane.setLayoutY(50);
            splitPane.prefWidthProperty().bind(root.widthProperty());
            splitPane.prefHeightProperty().bind(root.heightProperty().subtract(50));

            AnchorPane rRoot = new AnchorPane();
            AnchorPane wRoot = new AnchorPane();

            splitPane.getItems().addAll(rRoot,wRoot);

            rRoot.setMinWidth(200);
            rRoot.prefWidthProperty().bind(splitPane.getDividers().get(0).positionProperty());
            Button rBrowse = new Button();
            rRoot.getChildren().add(rBrowse);
            rBrowse.setText("Browse");
            DoubleBinding db0 = rRoot.widthProperty().subtract(55);
            db0.addListener(new javafx.beans.value.ChangeListener<Number>() {
                public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
                    rBrowse.setLayoutX(db0.getValue());
                }});
            Label rLabel = new Label("Reserve dir");
            rRoot.getChildren().add(rLabel);
            rLabel.setLayoutY(3);
            TextField rPath = new TextField();
            rRoot.getChildren().add(rPath);
            rPath.setLayoutX(60);
            rPath.prefWidthProperty().bind(rRoot.widthProperty().subtract(115));        

            wRoot.prefWidthProperty().bind(splitPane.widthProperty().subtract(splitPane.getDividers().get(0).positionProperty()));
            wRoot.setMinWidth(200);
            Button wBrowse = new Button();
            wRoot.getChildren().add(wBrowse);
            wBrowse.setText("Browse");
            DoubleBinding db1 = wRoot.widthProperty().subtract(55);
            db1.addListener(new javafx.beans.value.ChangeListener<Number>() {
                public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
                    wBrowse.setLayoutX(db1.getValue());
                }});
            Label wLabel = new Label("Working dir");
            wRoot.getChildren().add(wLabel);
            wLabel.setLayoutY(3);
            TextField wPath = new TextField();
            wRoot.getChildren().add(wPath);
            wPath.setLayoutX(64);
            wPath.prefWidthProperty().bind(wRoot.widthProperty().subtract(119));

            rBrowse.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle (ActionEvent e) {
                    System.out.println("Called");
                }
            });

            root.getChildren().addAll(splitPane);
            stage.setScene(scene);
            stage.show();
        }
    }
0

There are 0 answers