I have a main form (MainForm.fxml) that has its controller defined in the fxml file. In this same fxml file I have 2 subforms (Subform1.fxml and Subform2.fxml) that I have included with fx:include. Subform1 has a concrete controller. Subform2 is a general purpose 'select and edit' form with abstract code behind it. I want to display Subform2 with different concrete implementations of the abstract code depending on the context. If I define the controller in the fxml then it will not be general purpose anymore.
I am only using FXMLLoader to load the MainForm, and I can't find anywhere a way of changing the controller for the subforms. I have gone all around the houses trying different things. Any help would be much appreciated.
Updates to my question Thanks to James_D for the help so far. The definition of my Subform1 in the fxml file:
<children>
<!--<fx:include source="Subform1.fxml" />-->
<!-- <Subform1 controller="${ISubform}" /> -->
<Subform1 controller="${Subform1Controller}" />
<!-- <Subform1 /> -->
</children>
I have created an interface as follows:
package testsubforms;
public interface ISubform {
}
And this is my controller:
package testsubforms;
public class Subform1Controller implements ISubform {
public Subform1Controller() {
System.out.println("Inside Subform1Controller");
}
}
The following is my Subform1 class:
package testsubforms;
import java.io.IOException;
import javafx.beans.NamedArg;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.GridPane;
public class Subform1 extends GridPane {
private ObjectProperty controller;
public ObjectProperty controllerProperty() {
return this.controller;
}
public void setController(Subform1Controller controller) {
this.controllerProperty().set(controller);
}
public Subform1(@NamedArg("controller") Subform1Controller controller) throws IOException {
this.controller = new SimpleObjectProperty(this, "controller", controller);
FXMLLoader loader = new FXMLLoader(getClass().getResource("Subform1.fxml"));
loader.setRoot(this);
loader.setController(controller);
loader.load();
}
public Subform1() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Subform1.fxml"));
loader.setRoot(this);
loader.setController(this);
loader.load();
}
}
My current problem is the runtime error "javafx.fxml.LoadException: Cannot bind to untyped object" where I specify Subform1 in the fxml file. Any help to get this final piece in the jigsaw to work would be much appreciated. Once I get this last piece to work I will post the complete example for others to use later.