grunk
grunk

Reputation: 14938

JavaFx TabPane : One controller per Tab

I'm new with javafx and i'm trying to have one controller per tab in a tabpane. I found this answer : https://stackoverflow.com/a/19889980/393984 which lead me to do this :

Main.fxml

<TabPane fx:controller="sample.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/null">
  <tabs>
    <Tab text="Configuration">
      <content>
        <fx:include fx:id="mConfigTabPage" source="configTab.fxml"/>
      </content>
    </Tab>
    <Tab text="TODO">
      <content>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
      </content>
    </Tab>
  </tabs>
</TabPane>

configTab.fxml

<Pane fx:controller="sample.ConfigController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/null" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label layoutX="23.0" layoutY="22.0" text="API Key :" />
      <TextField layoutX="95.0" layoutY="18.0" fx:id="mAPIKey" />
   </children>
</Pane>

Controller.java

public class Controller {
    private Stage mStage;

    @FXML
    private ConfigController mConfigTabPage;

    public void Controller(){}

    public void setStage(Stage stage)
    {
        mStage = stage;
    }

    @FXML
    public void initialize() {
        System.out.println("CONTROLLER");
    }
}

ConfigController.java

public class ConfigController {
    public void ConfigController(){}

    @FXML
    public void initialize() {
        System.out.println("CONFIG CONTROLLER");
    }
}

My program is launching if i remove

@FXML
private ConfigController mConfigTabPage;

in the main controller.

But as soon as i add it i have the following exception :

java.lang.IllegalArgumentException: Can not set sample.ConfigController field sample.Controller.mConfigTabPage to javafx.scene.layout.AnchorPane

So i'm guessing that javafx trying to cast my controller into an AnchorPane and that causing the problem.

What should i do to be able to have a reference of each pane's controllers in my main controller ?

Upvotes: 0

Views: 2018

Answers (1)

Nikos Paraskevopoulos
Nikos Paraskevopoulos

Reputation: 40296

If you want the controller of something with fx:id="something" append the suffix Controller to your Java member field. So you have to use:

@FXML
private ConfigController mConfigTabPageController;

See reference.

Upvotes: 2

Related Questions