1

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();
    }   
}
Aman
  • 735
  • 1
  • 6
  • 19
Guenter
  • 13
  • 4

1 Answers1

1

When you load a tab "dynamically" via the createTab() method, you never call setMainWindowController(...) on the controller for that newly-created tab content. So in that controller, firstTabController is still null, and you get a Null Pointer Exception in the onAddTabBtnClicked method.

You can do

public void createTab() throws IOException {
    int numTabs = myTabPane.getTabs().size();
    Tab tab = new Tab("Tab " + (numTabs + 1));
    FXMLLoader loader = new FXMLLoader(getClass().getResource("NewTab.fxml"));
    Parent root = loader.load();
    NewTabController newTabController = loader.getController();
    newTabController.setMainWindowController(this);
    tab.setContent(root);
    myTabPane.getTabs().add(tab);
}

You don't really need to treat the first tab differently to the others; you could simply call the createTab() method from initialize() to make the first one, reducing the code a little (and using consistent techniques throughout):

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" />

FirstTabController:

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;

    public void initialize() {
        createTab();
    }

    public void createTab() throws IOException {
        int numTabs = myTabPane.getTabs().size();
        Tab tab = new Tab("Tab " + (numTabs + 1));
        FXMLLoader loader = new FXMLLoader(getClass().getResource("NewTab.fxml"));
        Parent root = loader.load();
        NewTabController newTabController = loader.getController();
        newTabController.setMainWindowController(this);
        tab.setContent(root);
        myTabPane.getTabs().add(tab);
    }
}

NewTab.fxml and NewTabController remain the same.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • Phew! - Thanks a lot for the detailed answer, James_D - works great :) I spent about a week with the issue, tried a lot and found myself in a big mess, at last ;) - your solution helps me a lot :) – Guenter Aug 22 '17 at 15:26