0

I am developing a JavaFX application that has several "child" FXML views. I am struggling to make sure all the children have access to the "root" layout.

Here is the basic hierarchy of my application:

RootLayout
  -> SubLayout1
    -> UnknownLayout

SubLayout1 is created from an FXML file when a button is clicked on the RootLayout.

Within the SubLayout1 controller, I have code that will instantiate and load the UnknownLayout (FXML) and UnknownLayoutController (associated controller) files dynamically:

FXMLLoader loader = new FXMLLoader(getClass().getResource(file.toString()));
loader.setController(controller);
loader.setRoot(this);
loader.load();

Both file and controller are assigned just prior to the FXML loading being done.

Since I am doing this dynamically, I do not know what the actual name of UnknownLayout or its controller will be.

My issue is that I can't figure out how to pass an instance of the RootLayout controller to the UnknownLayoutController so it can access the methods within that controller. I do have a method in each UnknownLayoutController class that will allow me to pass the controller of RootLayout to it:

public void setRootLayoutController (RootLayoutController rootController) {}

Passing the RootController to SubLayout1 isn't an issue because I can pass it with the constructor. But without knowing the actual name of UnknownLayout, how would I ever call that method? Is there a way to call the new controller's constructor from within the FXMLLoader block? Any "best practice" way to accomplish this?

My controller reference is created here:

StringBuilder controller = new StringBuilder();
controller.append(title + "Controller.java");

Thank you in advance for any help you can provide!

EDIT: I have tried several other suggestions to accomplish this, including Passing Parameters JavaFX FXML, but everything I've found assumes I know the name of the new controller prior to runtime.

I also looked into implementing an interface in UnknownLayoutController, but I then get a NullPointerException error on this code:

UnknownLayoutInterface subController = loader.getController();
subController.setRootController(rootController);

The controller variable is a String that does hold the name of the controller in question, but is there even a way to convert the String to an Object of the same name and access it that way?

(I am now defining the FXMLs controller in the FXML fx:controller tag instead of within the code).

Community
  • 1
  • 1
Zephyr
  • 9,885
  • 4
  • 28
  • 63
  • *Is there a way to call the new controller's constructor from within the FXMLLoader block?* Could you clarify this? How do you create the controller? Using `fx:controller` in the fxml? Using the factory/strategy/command pattern? – fabian Jul 23 '16 at 14:38

1 Answers1

0

I was able to solve the situation by simplifying my code and determining the class type based on the filename (which was dynamically determined):

Class subLayout;
  SubLayout2Interface subInterface = null;
  try {
    subLayout = Class.forName([Full Package Name] + subLayoutTitle);
    subInterface = (SubLayout2Interface) subLayout.newInstance();
  } catch (Exception e) {
    e.printStackTrace();
}

subInterface.setRootController(this);
Zephyr
  • 9,885
  • 4
  • 28
  • 63