Loading multiple fxml in javafx

2019-05-03 20:17发布

I've been searching for a while and I haven't been able to get what I wanted to do. I've been using the javafx framework to switch fxml in javafx in this thread 'Loading new fxml in the same scene' with some success. I mean, I have a main window, and this window have two different areas, the MAIN area, and the CONTENT area.

The MAIN area will remain the same in the whole application, but the CONTENT area, will be changing constantly. My MAIN area has just a toolbar, and depending which button in the toolbar you click, the fxml (and the behavior) in the CONTENT area will change.

I already have that behavior and it works like a charm using that said framework.

The thing is, that I want that all my CONTENT areas could have their own MAIN areas too, and an own CONTENT areas too. And depending on what you do in that second MAIN areas, the fxml and the behavior in that second CONTENT areas changes too.

I don't know if the framework I'm using is enough and if I will be able to get what I want to do using it.

Do you know another different framework that could help me to get that functionality?

Thanks a lot in advance!

1条回答
疯言疯语
2楼-- · 2019-05-03 21:05

Every .fxml that you load can have its own controller. Every controller can have custom action and loading code just like your main one does.

Basically, JavaFX is modular. If something is possible in your main pane, then it's possible in all its children too.

Generally, in UI-based frameworks, it is a wise idea to keep a hierarchy of controllers that mirrors the hierarchy of their respective components. If you want to get the child controller for the child element (for example, in order to pass the reference to the parent controller), then you can't rely on the FXML.load() shortcut, you have to use the longer syntax of:

FXMLLoader loader = new FXMLLoader(getClass().getResource("your.fxml"));
YourPaneType pane = loader.load();
YourControllerType controller = loader.getController();
查看更多
登录 后发表回答