user3111525
user3111525

Reputation: 5203

JavaFX: how to layout children of Group to fill width and height

I am trying to figure out how to maximize the width and height of a node packed in a Group. The main reason I want to pack into a Group is that I want to include a TranslateTransition from my treeView to listView and back. To do that I need a dummy Node that will be moved back and forth and that node must have absolute positioning.

According to this SO question I need to, for example, use a Group which I am trying to achieve in the MWE below.

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.SplitPane;
import javafx.scene.control.ToolBar;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import static javafx.collections.FXCollections.observableArrayList;

public class MWE extends Application {

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

    public void start(Stage primaryStage) throws Exception {
        final VBox top = new VBox(new MenuBar(new Menu("File")), new ToolBar(new Button("button1")));
        final ToolBar bottom = new ToolBar(new Label("status"));
        final SplitPane main = new SplitPane(new SplitPane(createSplitPaneWithTreeView()), new VBox());
        //main.setPrefSize(1920,1080);
        final Group center = new Group(main);
        main.setDividerPositions(0.40, 0.60);
        BorderPane borderPane = new BorderPane(center, top, new Label(), bottom, null);
        show(primaryStage, borderPane);

    }

    private SplitPane createSplitPaneWithTreeView() {
        final TreeItem<String> root = new TreeItem<>("Foo");
        root.getChildren().addAll(new TreeItem<>("Item 1"), new TreeItem<>("Item 2"));
        final TreeView<String> treeView = new TreeView<>(root);
        SplitPane splitPane = new SplitPane(treeView, new ListView<>(observableArrayList("List item 1", "List item 2")));
        splitPane.setOrientation(Orientation.VERTICAL);
        return splitPane;
    }

    private void show(Stage primaryStage, Parent parent) {
        primaryStage.setScene(new Scene(parent, 1920, 1080));
        primaryStage.show();
    }

}

I want to basically avoid setting the preferred size of main via hardcoding. I know it is also possible to bind the widthProperty/heightProperty of the splitpane to the root, but is there any other elegant solution?

Upvotes: 1

Views: 5980

Answers (1)

404KnowledgeNotFound
404KnowledgeNotFound

Reputation: 181

If you really want to use a Group there is no way around setting or binding the prefWidth/Height of your main Splitpane. The Group resizes itself to the prefSize of its content after all. I dont understand however why you find it unelegant to bind your main to the scene width and height like so:

primaryStage.setScene(new Scene(borderPane, 1920, 1080));
main.prefHeightProperty().bind(primaryStage.getScene().heightProperty());
main.prefWidthProperty().bind(primaryStage.getScene().widthProperty());
primaryStage.show();

It will always resize with your Window too, its the behaviour you want but not the way, why?

Upvotes: 3

Related Questions