绑定宽度和动态加载FXML的高度 [英] Bind width and height of dynamically loaded fxml

查看:196
本文介绍了绑定宽度和动态加载FXML的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我已经在运行时使用动态加载FXML文件的问题。一旦他们被添加到一个窗格,他们不调整使用全widht&安培;该窗格的高度。

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 pane.

我用这个方法在我的窗格中加载FXML:

I use this method to load a FXML in my Pane:

public void showContentPane(String sURL){
    //1. sURL can be something like "/GUI/home/master/MasterHome.fxml"
    //2. getContentPane() returns following object: Pane pContent
    try {
        URL url = getClass().getResource(sURL);

        getContentPane().getChildren().clear();
        Node n = (Node) FXMLLoader.load(url, ResourceBundle.getBundle("src.bundles.bundle", getLocale()));

        getContentPane().getChildren().add(n);
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}

的FXML被加载和工作,因为它应该不过我注意到FXML(加入作为在这种情况下,节点)不beeing调整为使用内容窗格的整个高度和宽度(如果我在$打开FXML与场景生成器p $ PVIEW模式,它完美地调整大小)。这是一种错误的做法还是有一个简单的方法,明确我没找到?

The FXML gets loaded and works as it should, however I notice the FXML (added as a Node in this case) is not beeing resized to use the full height and width of the content pane (if I open the FXML in preview mode with Scene Builder, it resizes perfectly). Is this a wrong approach or is there an easy way which clearly I did not find?

在此先感谢!

推荐答案

我调整了code与线索安迪给我。我改变了pContent对象到AnchorPane,而不是一个窗格。方法如下工作:

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());
    }
}

提示:请确保您的加载FXML文件时不要使用固定的宽度或高度变量

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

这篇关于绑定宽度和动态加载FXML的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆