Karim Manaouil
Karim Manaouil

Reputation: 1249

How to automatically resize JavaFX containers when window is maximized?

Basically, I have the following document:

<?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXTabPane?>
<?import com.jfoenix.controls.JFXTreeView?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

   <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="566.0" prefWidth="753.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.controller">
    <center>
        <JFXTabPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" BorderPane.alignment="CENTER">
            <tabs>
                <Tab text="TAB">
                    <content>
                        <AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                            <children>
                                <VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="495.0" prefWidth="753.0">
                                    <children>
                                        <StackPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="150.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
                                            <children>
                                                <HBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" nodeOrientation="LEFT_TO_RIGHT" prefHeight="100.0" prefWidth="200.0">
                                                    <children>
                                                        <JFXTreeView fx:id="treeView" />
                                                        <TableView  maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="277.0" prefWidth="753.0">
                                                            <columns>
                                                                <TableColumn maxWidth="-1.0" text="COL" />
                                                                <TableColumn text="COL" />
                                                                <TableColumn text="COL" />
                                                                <TableColumn text="COL" />
                                                                <TableColumn minWidth="0.0" text="COL" />
                                                                <TableColumn text="COL" />
                                                            </columns>
                                                            <columnResizePolicy>
                                                                <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                                                            </columnResizePolicy>
                                                        </TableView>
                                                    </children>
                                                </HBox>
                                            </children>
                                        </StackPane>
                                    </children>
                                </VBox>
                            </children>
                        </AnchorPane>
                    </content>
                </Tab>
            </tabs>
        </JFXTabPane>
    </center>
</BorderPane>

The main class to load the FXML document:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {

        BorderPane parent = (BorderPane)  FXMLLoader.load(getClass().getResource("document.fxml"));
        Scene scene = new Scene(parent);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

When i maximize the window, the table and the rest of the containers don't resize accordingly. I tried to set Max Width and Max Height for all the containers to MAX_VALUE in SceneBuilder but nothing really happens.

Is there a way to automatically expand the containers so that they can use all the space when the window is maximized ?

Upvotes: 3

Views: 8266

Answers (2)

Karim Manaouil
Karim Manaouil

Reputation: 1249

After spending some time investigating the problem. The steps that have corrected the issue are :

1- Setting all the anchor pane constraints to 0 (In the VBox tag) will allow for the child container to occupy all the space provided by the parent.

<VBox AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">

2- Setting the Hgrow attribute of the TabeView to Always will allow for the table to grow horizontally whenever it finds space.

3- For the rest of the containers, keeping the default settings and setting Max Height and Max Width to USE_COMPUTED_SIZE will do the trick.

Upvotes: 4

c0der
c0der

Reputation: 18792

Here is a start: a border pane that will grow with scene:

public void start(Stage stage) {

    BorderPane root = new BorderPane();
    root.setStyle("-fx-background-color: yellow");
    root.setCenter(new Text("center"));
    root.setTop(new Text("top"));
    root.setBottom(new Text("bottom"));

    Scene scene = new Scene(root);
    root.prefHeightProperty().bind(scene.heightProperty());
    root.prefWidthProperty().bind(scene.widthProperty());
    stage.setScene(scene);
    stage.show();
}

Upvotes: 0

Related Questions