JavaFX Panel inside Panel auto resizing

前端 未结 6 409
悲&欢浪女
悲&欢浪女 2020-12-04 14:32

I have a JavaFX application which has only one FXML file. In this file I have one AnchorPane which has a StackPane inside it. Here is the screenshot:

相关标签:
6条回答
  • 2020-12-04 14:42

    Also see here

    @FXML
    private void mnuUserLevel_onClick(ActionEvent event) {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("DBedit.fxml"));
        loader.setController(new DBeditEntityUserlevel());
    
        try {
               Node n = (Node)loader.load();
               AnchorPane.setTopAnchor(n, 0.0);
               AnchorPane.setRightAnchor(n, 0.0);
               AnchorPane.setLeftAnchor(n, 0.0);
               AnchorPane.setBottomAnchor(n, 0.0);
               mainContent.getChildren().setAll(n);
        } catch (IOException e){
               System.out.println(e.getMessage());
        }
    }
    

    The scenario is to load a child fxml into parent AnchorPane. To make the child to stretch in accords to its parent use AnChorPane.setxxxAnchor command.

    0 讨论(0)
  • 2020-12-04 14:45

    After hours of searching and testing finally got it just after posting the question!

    You can use the "AnchorPane.topAnchor, AnchorPane.bottomAnchor, AnchorPane.leftAnchor, AnchorPane.rightAnchor" fxml commands with the value "0.0" to fit/stretch/align the child elements inside a AnchorPane. So, these commands tell to child element to follow its parent while resizing.

    My updated code Main.fxml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import java.lang.*?>
    <?import java.util.*?>
    <?import javafx.scene.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    
    <AnchorPane fx:id="anchorPane" xmlns:fx="http://javafx.com/fxml" fx:controller="app.MainController">
        <!--<StackPane fx:id="stackPane" ></StackPane>--> <!-- replace with the following -->
        <StackPane fx:id="stackPane" AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" ></StackPane>
    </AnchorPane>
    

    Here is the result:

    enter image description here

    For api documentation: http://docs.oracle.com/javafx/2/api/javafx/scene/layout/AnchorPane.html

    0 讨论(0)
  • 2020-12-04 14:51

    It is quite simple because you are using the FXMLBuilder.

    Just follow these simple steps:

    1. Open FXML file into FXMLBuilder.
    2. Select the stack pane.
    3. Open the Layout tab [left side tab of FXMLBuilder].
    4. Set the sides value by which you want to compute the pane size during stage resize like TOP, LEFT, RIGHT, BOTTOM.
    0 讨论(0)
  • 2020-12-04 14:54

    I was designing a GUI in SceneBuilder, trying to make the main container adapt to whatever the window size is. It should always be 100% wide.

    This is where you can set these values in SceneBuilder:

    AnchorPane Constraints in SceneBuilder

    Toggling the dotted/red lines will actually just add/remove the attributes that Korki posted in his solution (AnchorPane.topAnchor etc.).

    0 讨论(0)
  • 2020-12-04 14:56

    If you are using Scene Builder, you will see at the right an accordion panel which normally has got three options ("Properties", "Layout" and "Code"). In the second one ("Layout"), you will see an option called "[parent layout] Constraints" (in your case "AnchorPane Constrainsts").

    You should put "0" in the four sides of the element wich represents the parent layout.

    0 讨论(0)
  • 2020-12-04 14:58

    No need to cede.

    just select pane ,right click then select Fit to parent.

    It will automatically resize pane to anchor pane size.

    0 讨论(0)
提交回复
热议问题