01secn
01secn

Reputation: 1

How can I create multiple collapsible panes in Java FX?

How can I create multiple collapsible panes in Java FX?

The end result would be two panes on, say, the right-hand side of screen. If one is open, it takes up a third of the screen on the right. If both are open, then one will fill up the top-right-hand corner, and the other the bottom right-hand corner. This would not result in the content of each pane being shrunk, but some of it hidden, with the user able to scroll down each pane to see the hidden content.

Each can be opened / closed by clicking a button on the screen.

Thank you!

Upvotes: 0

Views: 3935

Answers (1)

James_D
James_D

Reputation: 209428

I'm not exactly sure I have the description right, but this should be enough to get you started. Use a TitledPane for the collapsible panes, and put them inside something that can evenly distribute the vertical space, such as a GridPane with appropriate RowConstraints. Put the GridPane in the right side of a BorderPane.

SSCCE:

import javafx.application.Application;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TitledPaneInGridExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        GridPane grid = new GridPane();
        grid.add(createTitledPane("Pane 1"), 0, 0);
        grid.add(createTitledPane("Pane 2"), 0, 2);

        RowConstraints top = new RowConstraints();
        top.setValignment(VPos.TOP);
        top.setPercentHeight(100.0 / 3.0);

        RowConstraints middle = new RowConstraints();
        middle.setValignment(VPos.CENTER);
        middle.setPercentHeight(100.0 / 3.0);

        RowConstraints bottom = new RowConstraints();
        bottom.setValignment(VPos.BOTTOM);
        bottom.setPercentHeight(100.0 / 3.0);

        grid.getRowConstraints().addAll(top, middle, bottom);

        BorderPane root = new BorderPane(new Label("Content"), null, grid, null, null);
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }

    private TitledPane createTitledPane(String title) {
        TitledPane expandable = new TitledPane();
        expandable.setText(title);
        VBox content = new VBox(5);
        for (int i=1; i<=20; i++) {
            content.getChildren().add(new Label("Item "+i));
        }
        ScrollPane scroller = new ScrollPane();
        scroller.setContent(content);
        expandable.setContent(scroller);
        expandable.setExpanded(false);
        return expandable ;
    }

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

Upvotes: 1

Related Questions