Search code examples
javafxresizescenestage

Can i resize scene with the size of window JavaFx without binding each widget?


In my application, I am switching fxml files for changing the views because I don't properly know the concepts of Scene and Parent roots. all of my fxml files have an AnchorPane(600,400). This is how my start method looks like.

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("loginpage.fxml"));
    primaryStage.setTitle("Eye Ratina Scanner");
    primaryStage.setScene(new Scene(root, 600, 400));
    primaryStage.show();
}

this is how I am changing views.

AnchorPane pane = FXMLLoader.load(getClass().getResource("../loginpage.fxml"));
        dashpane.getChildren().setAll(pane) //dashpane is AnchorPane

I made the whole app in size of 600:400. Now I want that when I click on the window maximize button, all of the components resize larger and preserve the ratio. two images are attached below to show what's happening.when i click on maximize

normal 600:400 view

now I don't want to bind each button, imageview, anchorpane, text fields, labels, and so on. It will take too long since I have around 42 fxml files. Can we achieve our results by a single snippet? I didn't find any other post having exactly the same issue.

thanks for answering in advance. I totally appreciate your help.


Solution

  • Here is a sample layout. It uses a VBox as the root. It uses an AnchorPane. Inside the AnchorPane is a GridPane. You will still need to do work to make the ImageView and Label text grow as the Stage gets bigger.

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.ColumnConstraints?>
    <?import javafx.scene.layout.GridPane?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.layout.RowConstraints?>
    <?import javafx.scene.layout.VBox?>
    <?import javafx.scene.shape.Circle?>
    <?import javafx.scene.text.Font?>
    
    
    <VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: lightblue;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
       <children>
          <HBox maxHeight="400.0" prefHeight="75.0" VBox.vgrow="SOMETIMES">
             <children>
                <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Dashboard" HBox.hgrow="ALWAYS">
                   <font>
                      <Font name="System Bold" size="19.0" />
                   </font>
                </Label>
                <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Analytics" HBox.hgrow="ALWAYS">
                   <font>
                      <Font name="System Bold" size="19.0" />
                   </font>
                </Label>
                <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Settings" HBox.hgrow="ALWAYS">
                   <font>
                      <Font name="System Bold" size="19.0" />
                   </font>
                </Label>
                <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Notifications" HBox.hgrow="ALWAYS">
                   <font>
                      <Font name="System Bold" size="19.0" />
                   </font>
                </Label>
                <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Logout" HBox.hgrow="ALWAYS">
                   <font>
                      <Font name="System Bold" size="19.0" />
                   </font>
                </Label>
             </children>
          </HBox>
          <AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: grey;" VBox.vgrow="ALWAYS">
             <children>
                <GridPane hgap="20.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" vgap="20.0" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="20.0">
                  <columnConstraints>
                      <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="10.0" prefWidth="100.0" />
                      <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="10.0" prefWidth="100.0" />
                    <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="10.0" prefWidth="100.0" />
                    <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="10.0" prefWidth="100.0" />
                  </columnConstraints>
                  <rowConstraints>
                    <RowConstraints maxHeight="1.7976931348623157E308" minHeight="10.0" prefHeight="30.0" vgrow="ALWAYS" />
                      <RowConstraints maxHeight="1.7976931348623157E308" minHeight="10.0" prefHeight="30.0" vgrow="ALWAYS" />
                  </rowConstraints>
                   <children>
                      <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: white;">
                         <children>
                            <Circle fill="DODGERBLUE" radius="25.0" stroke="BLACK" strokeType="INSIDE" VBox.vgrow="ALWAYS" />
                            <Label text="Label" />
                            <Label text="Label" />
                         </children>
                      </VBox>
                      <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: white;" GridPane.columnIndex="1">
                         <children>
                            <Circle fill="DODGERBLUE" radius="25.0" stroke="BLACK" strokeType="INSIDE" />
                            <Label text="Label" />
                            <Label text="Label" />
                         </children>
                      </VBox>
                      <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: white;" GridPane.columnIndex="3" GridPane.rowIndex="1">
                         <children>
                            <Circle fill="DODGERBLUE" radius="40.0" stroke="BLACK" strokeType="INSIDE" />
                         </children>
                      </VBox>
                      <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: white;" GridPane.columnIndex="2" GridPane.rowIndex="1">
                         <children>
                            <Circle fill="DODGERBLUE" radius="25.0" stroke="BLACK" strokeType="INSIDE" />
                            <Label text="Label" />
                            <Label text="Label" />
                         </children>
                      </VBox>
                      <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: white;" GridPane.columnIndex="1" GridPane.rowIndex="1">
                         <children>
                            <Circle fill="DODGERBLUE" radius="25.0" stroke="BLACK" strokeType="INSIDE" />
                            <Label text="Label" />
                            <Label text="Label" />
                         </children>
                      </VBox>
                      <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: white;" GridPane.rowIndex="1">
                         <children>
                            <Circle fill="DODGERBLUE" radius="25.0" stroke="BLACK" strokeType="INSIDE" />
                            <Label text="Label" />
                            <Label text="Label" />
                         </children>
                      </VBox>
                      <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: white;" GridPane.columnIndex="3">
                         <children>
                            <Circle fill="DODGERBLUE" radius="25.0" stroke="BLACK" strokeType="INSIDE" />
                            <Label text="Label" />
                            <Label text="Label" />
                         </children>
                      </VBox>
                      <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: white;" GridPane.columnIndex="2">
                         <children>
                            <Circle fill="DODGERBLUE" radius="25.0" stroke="BLACK" strokeType="INSIDE" />
                            <Label text="Label" />
                            <Label text="Label" />
                         </children>
                      </VBox>
                   </children>
                </GridPane>
             </children>
          </AnchorPane>
       </children>
    </VBox>
    

    enter image description here

    enter image description here