我希望可以得到关于在TabPane中为每个选项卡使用fx:include语句的答案。我已经轻松地让内容显示出来,但是无论我如何构造,都会给我一个NullPointerException异常,从而无法引用相关控制器类的方法。包含的FXML布局的控制器既没有构造函数也没有initialize()方法,它们需要吗?我尝试了一些不同的事情,但总是得到相同的异常。
我所做的简单操作是向选项卡添加更改侦听器,当按下选项卡时,我想用从全局可访问的ArrayList获取的某些值填充一些文本字段。注意:ArrayList不是问题,使用主控制器执行此操作可以正常工作。
我很快就会添加代码示例,但现在不能。请让我知道您是否需要更多信息,否则我将在今天晚些时候发布代码。
*编辑:这是我的代码示例,摘自StackOverflow上的另一个帖子。 JavaFX TabPane - One controller for each tab
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);
}
}
主控制器,我想引用子控制器。
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();
}
});
}
}
主视图的.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>
FooTab:
public class FooTabController {
@FXML private Label lblText;
public void handleButton() {
lblText.setText("Byebye!");
}
}
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>
酒吧账单:
public class BarTabController {
@FXML private Label lblText;
public void handleButton() {
lblText.setText("Byebye!");
}
}
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>
上面的onAction方法对于FooTab和BarTab都可以与它们各自的按钮一起使用。当这个方法(handleButton)从Main控制器引用时,我就会得到一个异常。