Olivier Hanny
Olivier Hanny

Reputation: 35

How to make a component fill a splitpane on JavaFX?

I would like to add a component on one side of a JavaFX splitpane. How to make it fill the side panel of the splitpane ? It looks like hgrow and vgrow are not available on a splitpane.

In fact, I'm trying to add a titled pane in a split pane. Here's the code :

public void start(Stage stage) throws Exception {
    TitledPane titledPane1 = new TitledPane("Panel 1", new Group());
    TitledPane titledPane2 = new TitledPane("Panel 2", new Group());

    SplitPane rootPane = new SplitPane();
    rootPane.getItems().add(titledPane1);
    rootPane.getItems().add(titledPane2);

    Scene scene = new Scene(rootPane);
    stage.setWidth(400);
    stage.setHeight(300);
    stage.setScene(scene);
    stage.show();

}

Upvotes: 0

Views: 3725

Answers (2)

Geoff
Geoff

Reputation: 568

titledPane1.setMaxHeight(Double.MAX_VALUE);

Also solves the problem in the cases I've seen.

Upvotes: 1

ItachiUchiha
ItachiUchiha

Reputation: 36722

You should use a Pane instead of a Group.

The javadoc on Group says,

A Group will take on the collective bounds of its children and is not directly resizable.

Edit (as per user comments)

The problem here is not with Pane but with TitledPane, since TitledPane extends Labeled

You can bind the titledPane's height with the splitpane's height using

titledPane1.prefHeightProperty().bind(rootPane.heightProperty());

A working example with TiltledPane height bind and other with a Pane added to SplitPane

public class StoreNumbers extends Application {

    public void start(Stage stage) throws Exception {
        Pane pane1 = new Pane();
        Pane pane2 = new Pane();
        TitledPane titledPane1 = new TitledPane("Panel 1", pane1);
        TitledPane titledPane2 = new TitledPane("Panel 2", pane2);
        SplitPane rootPane = new SplitPane();
        rootPane.getItems().add(titledPane1);
        rootPane.getItems().add(pane2);

        Scene scene = new Scene(rootPane);
        stage.setWidth(400);
        stage.setHeight(300);
        stage.setScene(scene);
        stage.show();
        pane1.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, null, null)));
        pane2.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, null, null)));
        titledPane1.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.DASHED, null, null)));
        titledPane2.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.DASHED, null, null)));
        titledPane1.prefHeightProperty().bind(rootPane.heightProperty());
    }

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

Upvotes: 0

Related Questions