JavaFX 1 FXML File with Multiple Different Controllers?

ぃ、小莉子 提交于 2019-12-18 13:23:14

问题


There are two different stages in my application that are help screens that use the same FXML file. Rather than create 2 FXML files, I would like to use just one and have two controllers that call the same fxml.

The only problem is that the Controller is assigned in the FXML file. So, is there a way to change the assigned controller with code in the Controller class itself?

I'd really like to avoid duplicating an FXML file just to change the Controller in each. Thanks in advance.



回答1:


You can remove the fx:controller="" assignment from the FXML file and assign the controller via the FXMLLoader during the load.

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Your.fxml"));
fxmlLoader.setController(this);

try
{
    fxmlLoader.load();
}
catch (IOException exception)
{
    throw new RuntimeException(exception);
}

Check out the Introduction to FXML section on custom components.



来源:https://stackoverflow.com/questions/15964832/javafx-1-fxml-file-with-multiple-different-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!