Bind width and height of dynamically loaded fxml

前端 未结 1 982
轮回少年
轮回少年 2021-01-24 18:49

At the moment I have an issue with dynamically loaded FXML files during runtime. Once they are added to a Pane, they do not resize to use the full widht & height of that pan

1条回答
  •  一向
    一向 (楼主)
    2021-01-24 19:22

    I adjusted the code with the clue Andy gave me. I changed the pContent object to a AnchorPane instead of a Pane. Method that works below:

    public void showContentPane(String sURL){
        //1. sURL can be something like "/GUI/home/master/MasterHome.fxml"
        //2. getContentPane() returns following object: AnchorPane pContent
        try {
            URL url = getClass().getResource(sURL);
    
            getContentPane().getChildren().clear();
    
            //create new AnchorPane based on FXML file
            AnchorPane n = (AnchorPane) FXMLLoader.load(url, ResourceBundle.getBundle("src.bundles.bundle", getLocale()));
    
            //anchor the pane
            AnchorPane.setTopAnchor(n, 0.0);
            AnchorPane.setBottomAnchor(n, 0.0);
            AnchorPane.setLeftAnchor(n, 0.0);
            AnchorPane.setRightAnchor(n, 0.0);
    
            getContentPane().getChildren().add(n);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
    

    Hint: make sure that in your loaded FXML file you do not use fixed width or height variables.

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