Multiple controller for one FXML file

15,426

Solution 1

I recommend to create custom controls - use several smaller FXML files rather than one big FXML file.

Here is the tutorial:

http://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm

Solution 2

During the loading of your FXML markup, there is only the provision to have one controller specified for your scene graph. You are able to load other FXML markup files and nest controllers, but I don't think that's what you're asking.

To my mind, it doesn't matter anyway.

You can write any number of classes in any number of files and have them interact with your controller class just like any Java class can interact with any other Java class.

I really can't think of any particularly good reason why it would be useful to specify multiple controllers for a scene graph at load time.

Solution 3

You can add an fxml file as a node into another fxml , both with diferent controllers with include option in scene builder

Share:
15,426
RN Hindarwan
Author by

RN Hindarwan

Updated on June 09, 2022

Comments

  • RN Hindarwan
    RN Hindarwan almost 2 years

    I have one fxml file controlled by one controller. I want to separate the controller into multiple parts as if placed in one controller makes a long code. Unfortunately, fx:controller can only be placed in the root. Are there any suggestions for me how to separate the controller?

  • Tomas Bisciak
    Tomas Bisciak about 10 years
    @Puce If i choose this custom control aproach will i be still able to use scene builder with my fxml file ? And will it override my custom fxml settings over and over again when i save it from scene builder?
  • Puce
    Puce about 10 years
    @TomasBisciak Yes, the SceneBuilder supports the fx:root construct. It should behave in the same way as with non-fx:root FXML files.