1

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();
        }
    }
ZygD
  • 22,092
  • 39
  • 79
  • 102
klapeyron
  • 502
  • 7
  • 18
  • Example: https://www.dropbox.com/sh/j7xz40fzwbbh2fy/AAA9mmm2msgBAh9PbKlmKn4Va?dl=0 – klapeyron Jun 20 '15 at 10:10
  • 1
    Why don't you place the Label, TextField and Button inside a HBox and then add it to the AnchorPane? This would save you a lot of effort on bindings. – ItachiUchiha Jun 20 '15 at 10:30
  • Thanks, I'll try and write here about results. – klapeyron Jun 20 '15 at 12:18
  • Thank you! This works well with Hbox. – klapeyron Jun 20 '15 at 13:00
  • 1
    You are probably running into http://stackoverflow.com/questions/23785816/javafx-beans-binding-suddenly-stops-working/23788155#23788155 Using an appropriate layout instead of trying to bind everything should fix things. You can set the anchors on the split pane (e.g. `AnchorPane.setLeftAnchor(splitPane, 0.0);` etc) too, instead of binding the split pane's width etc. – James_D Jun 20 '15 at 13:00

0 Answers0