从动态添加的Tab中访问TabPane

时间:2017-08-22 08:10:11

标签: javafx tabs javafx-8

我是FXML的新手,我经常搜索TabPanes找到解决问题的方法。在下面的示例中(基于JavaFX: Adding a new tab from a tab controller中的示例)我想为TapPane添加一些标签 - 从"包含" -tab(标签1)中添加新标签isn&#39 ; ta问题,但是当我尝试从动态创建的选项卡(例如选项卡2)中添加选项卡时,我总是得到一个NullPointerException。 有谁能告诉我如何从动态创建的选项卡(表2)中访问FirstTab-Controller?

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();
    }   
}

1 个答案:

答案 0 :(得分:1)

当您加载标签&#34;动态&#34;通过createTab()方法,您永远不会在控制器上为新创建的选项卡内容调用setMainWindowController(...)。因此,在该控制器中,firstTabController仍然为空,并且您在onAddTabBtnClicked方法中得到空指针异常。

你可以做到

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);
}

你真的不需要将第一个标签与其他标签区别对待;你可以简单地从createTab()调用initialize()方法制作第一个方法,稍微减少代码(并使用一致的技术):

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.fxmlNewTabController保持不变。