JavaFX如何将新的FXML内容注入当前场景 [英] JavaFX how to inject new FXML content to current Scene

查看:174
本文介绍了JavaFX如何将新的FXML内容注入当前场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中包含带标题和菜单的HomeScene.fxml文件。 HomeScene还有dashboardPane,应该在按下菜单按钮后动态更改。仪表板窗格内容应该从另一个fxml文件加载,例如'FinancesPane.fxml'或'SettingsPane.fxml'。

I have an app, which has HomeScene.fxml file with headers and menu. HomeScene has also dashboardPane, which should be changed dynamically after menu button is being pressed. Dashboard pane content should be loaded from another fxml file, lets say 'FinancesPane.fxml' or 'SettingsPane.fxml'.

我试图在HomeController中替换dashboardPane的内容:

Im trying to replace content of dashboardPane in HomeController:

@FXML
public void handleFinancesButtonAction() {
    FinancesPaneFactory paneFactory = new FinancesPaneFactory();
    dashBoardPane.getChildren().clear();
    dashBoardPane.getChildren().add(paneFactory.createPane());
}

我的FinancesPaneFactory如下所示:

My FinancesPaneFactory looks like this:

public class FinancesPaneFactory extends PaneFactory {

    private static final String PANE_TEMPLATE_PATH = "/sceneTemplates/FinancesPane.fxml";

    public FinancesPaneFactory() {
        super(PANE_TEMPLATE_PATH );
    }

    @Override
    protected Pane generatePane(FXMLLoader loader) {
        try {
            return (Pane) loader.load();
        } catch (IOException e) {
            throw new FatBirdRuntimeException("Unable to load FinancesPane", e);
        }
    }

}

待更清楚的是,这就是HomeScene的样子: HomeScene
这个空白区域是一个dashboardPane,当用户按下左侧菜单按钮时,应该用其他内容替换。

To be more clear, this is how HomeScene looks like: HomeScene . This empty space is a dashboardPane, and should be replaced with another content when user press the left menu button.

如何动态注入此内容?

How to inject this content dynamically?

推荐答案

是的,你应该这样做以保持场景图低,你将从更好的表现中受益,我所做的就是创造动态容器:

Yes, you should do this to keep scene graph low and you will benefit from better performance , what i do is create dynamic container :

@FXML
private ScrollPane dynamicNode;

滚动窗格是一个不错的选择。

Scroll pane is a good choice.

这是放到MainController。

This is put to MainController.

我有主控制器与其他控制器不同,主控制器实际上是我初始化的唯一一个,所以在你的主程序类中,无论你怎么称呼它:

I have main controller different from others , main controller is actually the only one i initialize, so in your main program class whatever you call it :

private static MainViewController mainViewController;

...

 private static BorderPane loadMainPane() throws IOException {

        FXMLLoader loader = new FXMLLoader();
        loader.setController(mainViewController);
        BorderPane mainPane = (BorderPane) loader.load(
                CsgoRr.class
                .getResourceAsStream(Info.Resource.FXML_FILE_MAIN));
        mainPane.getStylesheets().add(CsgoRr.class.getResource("path...style.css").toString());

        return mainPane;
    }

别忘了创建静态访问器,我通常不创建其他控制器这样,我在fxml中使用fx:controller来指定哪个控制器应该是哪个fxml,它通常很方便让mainController可访问。

Dont forget to create static accessor, other controllers that i have are usually not created this way , i use fx:controller in fxml to specify what controller should be for which fxml , its usually handy to have mainController accessable.

所以改变你的视图创建您的主控制器方法连接到您更改视图的菜单

So to change your views create in your main controller methods that are connected to your menu with whose you change views

@FXML
private void setViewPreferences() {
    setView(Info.Resource.FXML_FILE_PREFERENCES);
}

@FXML
private void setViewProductPage() {
    setView(Info.Resource.FXML_FILE_PRODUCT_PAGE);
}

目前在dynamicNode中帮助查看当前选择的是什么,其

Currently in dynamicNode is helper to see what exactly is the current selected, its

private String currentlyInDynamicPane;//not important

这是setView

   public void setView(String fxmlPath) {
         dynamicNode.setContent(getView(fxmlPath));
            currentlyInDynamicPane = fxmlPath;
   }


  public Node getView(String fxmlPath) {
        try {
            return new FXMLLoader(getClass().getResource(fxmlPath)).load();
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }

因此,当您单击左侧菜单时,您交换FXML文件,你可以确保你在开始时显示了一些默认的FXML,或者当菜单中没有任何内容被选中时。

So when you click left menu you swap FXML files, you can make sure that you have some default FXML shown at the start or when nothing in menu is selected as well.

这就是我这样做的方式,粗略。

This is the way i do it, roughly.

所以想想你的DASHBOARD是DynamicPane,

So think about YOUR DASHBOARD as DynamicPane,

这篇关于JavaFX如何将新的FXML内容注入当前场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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