mskwon
mskwon

Reputation: 21

How to load fxml file to full size in borderpane

My goal is get the size of user monitor and show FXML file accordingly. However, the window appears to fit the size but the FXML file does not. How do I fit the file into the BorderPane?

In addition, I hope it can be automatically resize when I change the screen size.

I wrote the code by referring to the following YouTube for window control. https://www.youtube.com/watch?v=UzlXUlTD1Lo

Main.java

public class Main extends Application {
FXMLLoader loader = new FXMLLoader();
StackPane pane = new StackPane();
BorderPane mainPane = new BorderPane();
String main="test.fxml";
Scene scene = new Scene(pane, Screen.getPrimary().getVisualBounds().getWidth(), Screen.getPrimary().getVisualBounds().getHeight());

@Override
public void start(Stage primaryStage) {
    try {
        primaryStage.setX(Screen.getPrimary().getVisualBounds().getMinX());
        primaryStage.setY(Screen.getPrimary().getVisualBounds().getMinY());
        mainPane.prefWidthProperty().bind(pane.widthProperty());
        mainPane.prefHeightProperty().bind(pane.heightProperty());
        mainPane = (BorderPane) loader.load(getClass().getResourceAsStream(main));

        pane.getChildren().add(mainPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    } 
    catch(Exception e) {
        e.printStackTrace();
    }
}

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

}

test.fxml

    <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="922.0" prefWidth="1314.0" style="-fx-background-color: white;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
   <top>
      <VBox BorderPane.alignment="CENTER">
         <children>
            <MenuBar prefHeight="18.0" prefWidth="1078.0" style="-fx-background-color: #f0efef;">
              <menus>
                <Menu mnemonicParsing="false" text="File">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Close" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Edit">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Delete" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Help">
                  <items>
                    <MenuItem mnemonicParsing="false" text="About" />
                  </items>
                </Menu>
              </menus>
            </MenuBar>
            <ToolBar prefHeight="43.0" prefWidth="1078.0" style="-fx-background-color: #b0aacd;">
               <items>
                  <Label prefHeight="26.0" prefWidth="72.0" text="&gt;&gt;&gt;">
                     <font>
                        <Font size="18.0" />
                     </font>
                  </Label>
                  <TextField fx:id="ip" prefHeight="29.0" prefWidth="198.0" />
                  <Button fx:id="start" mnemonicParsing="false" onAction="#onIp" prefHeight="29.0" prefWidth="88.0" style="-fx-background-color: #ddeedd;" text="start" />
               </items>
            </ToolBar>
         </children>
      </VBox>
   </top>
   <center>
      <VBox BorderPane.alignment="CENTER">
         <children>
            <AnchorPane prefHeight="221.0" prefWidth="1078.0">
               <children>
                  <Label layoutX="14.0" layoutY="11.0" prefHeight="34.0" prefWidth="210.0" text="Chapter1">
                     <font>
                        <Font name="Expo M" size="25.0" />
                     </font>
                  </Label>
                  <Separator layoutX="9.0" layoutY="46.0" prefHeight="3.0" prefWidth="1221.0" />
                  <Label layoutX="620.0" layoutY="57.0" text="age" AnchorPane.leftAnchor="620.0">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <Label layoutX="291.0" layoutY="57.0" text="name" AnchorPane.leftAnchor="291.0">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <Label layoutX="301.0" layoutY="99.0" text="choice" AnchorPane.leftAnchor="301.0">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="age" layoutX="677.0" layoutY="57.0" prefHeight="29.0" prefWidth="126.0" />
                  <ChoiceBox layoutX="416.0" layoutY="99.0" prefHeight="26.0" prefWidth="51.0" />
                  <Button fx:id="submit" layoutX="1120.0" layoutY="141.0" mnemonicParsing="false" style="-fx-background-color: #b0aacd;" text="submit">
                     <font>
                        <Font name="Ebrima Bold" size="15.0" />
                     </font>
                  </Button>
                  <TextField fx:id="name" layoutX="341.0" layoutY="57.0" prefHeight="29.0" prefWidth="126.0" />
                  <CheckBox fx:id="male" layoutX="615.0" layoutY="99.0" mnemonicParsing="false" text="male">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </CheckBox>
                  <CheckBox fx:id="female" layoutX="712.0" layoutY="99.0" mnemonicParsing="false" text="female">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </CheckBox>
               </children>
            </AnchorPane>
            <AnchorPane prefHeight="227.0" prefWidth="1078.0">
               <children>
                  <Label layoutX="14.0" layoutY="16.0" prefHeight="32.0" prefWidth="179.0" text="Chapter2">
                     <font>
                        <Font name="Expo M" size="25.0" />
                     </font>
                  </Label>
                  <Separator layoutX="14.0" layoutY="48.0" prefHeight="7.0" prefWidth="1225.0" />
                  <Label layoutX="176.0" layoutY="67.0" text="aaa">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="text1" editable="false" layoutX="285.0" layoutY="66.0" prefHeight="26.0" prefWidth="110.0" />
                  <Label layoutX="450.0" layoutY="67.0" text="ddd">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="text2" editable="false" layoutX="598.0" layoutY="66.0" prefHeight="26.0" prefWidth="110.0" />
                  <Label layoutX="767.0" layoutY="67.0" text="ggg">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="text3" editable="false" layoutX="900.0" layoutY="66.0" prefHeight="26.0" prefWidth="110.0" />
                  <Label layoutX="746.0" layoutY="155.0" text="Result">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="text8" editable="false" layoutX="900.0" layoutY="155.0" prefHeight="26.0" prefWidth="110.0" />
                  <Label layoutX="174.0" layoutY="110.0" text="bbb">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="text4" editable="false" layoutX="285.0" layoutY="110.0" prefHeight="26.0" prefWidth="110.0" />
                  <Label layoutX="458.0" layoutY="110.0" text="eee">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="text5" editable="false" layoutX="598.0" layoutY="110.0" prefHeight="26.0" prefWidth="110.0" />
                  <Label layoutX="178.0" layoutY="155.0" text="ccc">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="text6" editable="false" layoutX="285.0" layoutY="155.0" prefHeight="26.0" prefWidth="110.0" />
                  <Label layoutX="458.0" layoutY="155.0" text="fff">
                     <font>
                        <Font name="Ebrima" size="19.0" />
                     </font>
                  </Label>
                  <TextField fx:id="texxt7" editable="false" layoutX="598.0" layoutY="155.0" prefHeight="26.0" prefWidth="110.0" />
                  <Button fx:id="submit1" layoutX="1127.0" layoutY="170.0" mnemonicParsing="false" style="-fx-background-color: #b0aacd;" text="submit">
                     <font>
                        <Font name="Ebrima Bold" size="15.0" />
                     </font>
                  </Button>
               </children>
            </AnchorPane>
            <AnchorPane prefHeight="444.0" prefWidth="1078.0">
               <children>
                  <Label layoutX="491.0" layoutY="9.0" text="List" textAlignment="CENTER">
                     <font>
                        <Font name="Expo M" size="25.0" />
                     </font>
                  </Label>
                  <Separator layoutX="7.0" layoutY="40.0" prefHeight="14.0" prefWidth="1225.0" />
                  <TableView fx:id="List" layoutX="11.0" layoutY="56.0" maxHeight="-Infinity" prefHeight="380.0" prefWidth="1288.0">
                    <columns>
                      <TableColumn fx:id="No" prefWidth="34.0" style="-fx-background-color: white;" />
                        <TableColumn fx:id="a" prefWidth="83.0" style="-fx-background-color: white;" text="a" />
                        <TableColumn fx:id="b" prefWidth="256.0" style="-fx-background-color: white;" text="b" />
                        <TableColumn fx:id="c" prefWidth="90.0" style="-fx-background-color: white;" text="c" />
                        <TableColumn fx:id="d" prefWidth="89.0" style="-fx-background-color: white;" text="d" />
                        <TableColumn fx:id="e" prefWidth="135.0" style="-fx-background-color: white;" text="e" />
                        <TableColumn fx:id="f" prefWidth="87.0" style="-fx-background-color: white;" text="f" />
                        <TableColumn fx:id="g" prefWidth="75.0" text="g" />
                        <TableColumn fx:id="h" prefWidth="75.0" style="-fx-background-color: white;" text="h" />
                        <TableColumn fx:id="i" prefWidth="149.0" style="-fx-background-color: white;" text="i" />
                        <TableColumn fx:id="j" prefWidth="135.0" style="-fx-background-color: white;" text="j" />
                        <TableColumn fx:id="k" prefWidth="75.0" style="-fx-background-color: white;" text="k" />
                    </columns>
                  </TableView>
               </children>
            </AnchorPane>
         </children>
      </VBox>
   </center>
</BorderPane>

The FXML file is located in the middle, but not the screen size.

Upvotes: 0

Views: 403

Answers (1)

Zephyr
Zephyr

Reputation: 10253

There are a couple things to point out with your code:

  1. You are creating a new BorderPane and then overriding that when you load the FXML. No need to call BorderPane mainPane = new BorderPane()
  2. After loading the FXML's BorderPane, you're placing it into a StackPane for some reason, but never configure the StackPane on how you want its children laid out.
  3. There is no reason to have to determine the screen resolution/size like this. You can call either primaryStage.setMaximized(true) or primaryStage.setFullscreen(true), depending on whether you want the stage borders to be visible.

Consider the following code snippet that I've simplified a bit:

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            // Create the loader and pass our text.fxml reference to it
            FXMLLoader loader = new FXMLLoader(getClass().getResource("test.fxml"));

            // If you do not specify a controller in the FXML itself, you can do so here
            loader.setController(new Controller());

            // Set our Stage's scene and load the FXML at the same time
            primaryStage.setScene(new Scene(loader.load()));

            // Maximize the stage (or set to fullscreen
            primaryStage.setMaximized(true);
//            primaryStage.setFullScreen(true);

            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

The Result:

screenshot

Upvotes: 1

Related Questions