JavaFX 8 - 每个选项卡都有单独的FXML和控制器的Tabpanes和选项卡

Mån*_*vik 12 java model-view-controller javafx fxml fxmlloader

我希望得到一些关于在tabpane中为每个选项卡提供fx:include语句的答案.我已经轻松地管理内容以显示但是相关控制器类的引用方法只是给我一个nullpointerreference异常,无论我如何构造它.包含的FXML布局的控制器既没有构造函数也没有初始化()方法,是否需要它们?我尝试了一些不同的东西,但总是得到同样的例外.

我只是在tabpane中添加一个更改侦听器,当按下一个选项卡时,我想填充一些带有从全局可访问的arraylist获取的值的文本字段.注意:arraylist不是问题,使用主控制器执行此操作工作正常.

我将很快添加一个代码示例,但现在不能.如果您需要更多信息,请告诉我,否则我会在今天晚些时候发布代码.

*编辑,这是我的代码示例,取自StackOverflow上的另一个线程. JavaFX TabPane - 每个选项卡一个控制器

TestApp.java:

public class TestApp extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(new StackPane());

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/MainTestWindow.fxml"));
        scene.setRoot(loader.load());
        MainTestController controller = loader.getController();
        controller.init();

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Run Code Online (Sandbox Code Playgroud)

主控制器,我想引用子控制器.

public class MainTestController {

    @FXML private TabPane tabPane;
    // Inject tab content.
    @FXML private Tab fooTabPage;
    // Inject controller
    @FXML private FooTabController fooTabPageController;

    // Inject tab content.
    @FXML private Tab barTabPage;
    // Inject controller
    @FXML private BarTabController barTabPageController;

    public void init() {
        tabPane.getSelectionModel().selectedItemProperty().addListener((ObservableValue<? extends Tab> observable,
                                                                        Tab oldValue, Tab newValue) -> {
            if (newValue == barTabPage) {
                System.out.println("Bar Tab page");
                barTabPageController.handleButton();
            } else if (newValue == fooTabPage) {
                System.out.println("Foo Tab page");
                fooTabPageController.handleButton();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

主视图的.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.Tab?>

<TabPane fx:id="tabPane" fx:controller="controller.MainTestController" xmlns="http://javafx.com/javafx/8.0.40"
         xmlns:fx="http://www.w3.org/2001/XInclude">
    <tabs>
        <Tab fx:id="fooTabPage" text="FooTab">
            <fx:include source="fooTabPage.fxml"/>
        </Tab>
        <Tab fx:id="barTabPage" text="BarTab">
            <fx:include source="barTabPage.fxml"/>
        </Tab>
    </tabs>
</TabPane>
Run Code Online (Sandbox Code Playgroud)

FooTab:

public class FooTabController {
    @FXML private Label lblText;

    public void handleButton() {
        lblText.setText("Byebye!");
    }
}
Run Code Online (Sandbox Code Playgroud)

FooTab的.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>

<?import javafx.scene.control.Button?>
<VBox xmlns:fx="http://www.w3.org/2001/XInclude" fx:controller="controller.FooTabController">
    <Label fx:id="lblText" text="Helllo"/>
    <Button fx:id="btnFooTab" onAction="#handleButton" text="Change text"/>
</VBox>
Run Code Online (Sandbox Code Playgroud)

BarTab:

public class BarTabController {
    @FXML private Label lblText;

    public void handleButton() {
        lblText.setText("Byebye!");
    }
}
Run Code Online (Sandbox Code Playgroud)

BarTab的.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>

<?import javafx.scene.control.Button?>
<VBox xmlns:fx="http://www.w3.org/2001/XInclude" fx:controller="controller.BarTabController">
    <Label fx:id="lblText" text="Helllo" />
    <Button fx:id="btnBarTab" onAction="#handleButton" text="Change text"/>
</VBox>
Run Code Online (Sandbox Code Playgroud)

上面的onAction for FooTab和BarTab都可以使用它们各自的按钮.当这个方法(handleButton)是来自主控制器的引用时,那就是我得到异常的时候.

Jam*_*s_D 9

要为包含的FMXL文件注入控制器,您需要元素fx:id上的属性<fx:include>.该控制器将被注入到与现场"Controller"附加到fx:id值.

如果你想注入实际的Tab,你需要一个单独fx:id的.

所以:

<tabs>
    <Tab fx:id="fooTab" text="FooTab">
        <fx:include fx:id="fooTabPage" source="fooTabPage.fxml"/>
    </Tab>
    <Tab fx:id="barTab" text="BarTab">
        <fx:include fx:id="barTabPage" source="barTabPage.fxml"/>
    </Tab>
</tabs>
Run Code Online (Sandbox Code Playgroud)

@FXML private TabPane tabPane;
// Inject tab content.
@FXML private Tab fooTab;
// Inject controller
@FXML private FooTabController fooTabPageController;

// Inject tab content.
@FXML private Tab barTab;
// Inject controller
@FXML private BarTabController barTabPageController;
Run Code Online (Sandbox Code Playgroud)