5

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?

Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
corbenik
  • 126
  • 2
  • 8
  • Yes, this should be possible. Is this answer enough or do you have any specific problem? – Puce Sep 30 '12 at 22:27
  • Well, I guess that answers the specific question I made. Let me rephrase: Could someone provide an example of importing an FXML module into an application and inserting that node into the main application during runtime? – corbenik Oct 01 '12 at 00:29
  • It should not be much different than importing a single FXML. Can you show what you have tried and where you struggle? – Puce Oct 01 '12 at 07:56

1 Answers1

4

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

Community
  • 1
  • 1
Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141