Is there a way to modularize a JavaFX application?

2019-03-20 19:38发布

I've started toying with JavaFX 2 and I really like the ease with which one can create a UI with FXML. However, once you get past the basic examples and you need a UI with many windows, it seems illogical to have the definition for the whole application's UI in a single FXML file.

Is it possible to write separate components in separate FXML files and then include them as needed? Say for example that I wanted a window to popup when the user clicks on an item from the main menu; could I write the definition for that window in a separate FXML file, load it at runtime and add it to the node tree?

1条回答
做自己的国王
2楼-- · 2019-03-20 20:34

From FXML perspective there are two approaches:

  1. Simply use fx:include directive to split application into logical parts in different fxml files.

  2. More advanced way would be to implement parts of your application as separate "controls". Which will have their own (can be very simple) API to provide data and can be used in FXML or FX API like built-in controls. See next question for details how to implement that using FXML: How to pass object created in FXML Controller1 to Controller2 of inner FXML control

查看更多
登录 后发表回答