I'm new in FXML and I searched a lot to find a solution for my troubles with TabPanes. In the below example (based on the example in JavaFX: Adding a new tab from a tab controller) I want to add some tabs to a TapPane - adding a new tab from inside an "included"-tab (Tab 1) isn't a problem, but when I try to add a tab from inside a dynamically created tab (e.g. Tab 2), I always get a NullPointerException. Could anyone please tell me how I get access to the FirstTab-Controller from inside the dynamically created tab (Tab 2)?
TestingTabPane.java:
package testingtabs;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class TestingTabPane extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FirstTab.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
FirstTab.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<TabPane fx:id="myTabPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testingtabs.FirstTabController">
<tabs>
<Tab fx:id="FirstTab" text="Tab 1">
<fx:include source="NewTab.fxml" fx:id="newTab"/>
</Tab>
</tabs>
</TabPane>
FirstTabController.java:
package testingtabs;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
public class FirstTabController {
@FXML
public TabPane myTabPane;
@FXML
public NewTabController newTabController;
public void initialize() {
newTabController.setMainWindowController(this);
}
public void createTab() throws IOException {
int numTabs = myTabPane.getTabs().size();
Tab tab = new Tab("Tab " + (numTabs + 1));
Parent root = FXMLLoader.load(getClass().getResource("NewTab.fxml"));
tab.setContent(root);
myTabPane.getTabs().add(tab);
}
}
NewTab.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="testingtabs.NewTabController">
<children>
<Button fx:id="addTabBtn" layoutX="268.0" layoutY="187.0" mnemonicParsing="false" onAction="#onAddTabBtnClicked" text="add tab" />
</children>
</AnchorPane>
NewTabController.java:
package testingtabs;
import java.io.IOException;
import javafx.fxml.FXML;
public class NewTabController {
private FirstTabController firstTabController ;
public void setMainWindowController(FirstTabController mainWindowController) {
this.firstTabController = mainWindowController ;
}
@FXML
public void onAddTabBtnClicked() throws IOException {
firstTabController.createTab();
}
}