Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: SplitPane changes divider position on resize if there are items inside

Tags:

java

javafx

I have the following code:

public class JavaFxApplication2 extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        SplitPane splitPane=new SplitPane();
        VBox vBox1=new VBox();
        vBox1.setStyle("-fx-background-color: red");
        VBox vBox2=new VBox();
        vBox2.setStyle("-fx-background-color: blue");
        splitPane.getItems().add(vBox1);
        splitPane.getItems().add(vBox2);
        splitPane.getDividers().get(0).setPosition(0);
        Scene scene=new Scene(splitPane, 200, 400);
        stage.setScene(scene);
        stage.show();
    }
}

This is the output when I run application:
enter image description here
Now I resize and make stage larger:
enter image description here
As you see divider position now is not 0. How to disable divider position changing on resizing?

like image 667
Pavel_K Avatar asked Jan 23 '17 18:01

Pavel_K


1 Answers

All the nodes contained by the SplitPane become resized by default, hence the "wandering divider".

You can use setResizableWithParent to avoid resizing the left VBox:

SplitPane.setResizableWithParent(vBox1, false);

Sets a node in the SplitPane to be resizable or not when the SplitPane is resized. By default all node are resizable. Setting value to false will prevent the node from being resized.

If you do not want the right VBox to be resized, this method has to be called for that node also.

like image 194
DVarga Avatar answered Nov 20 '22 04:11

DVarga