【发布时间】:2017-01-02 23:51:03
【问题描述】:
我希望得到一些关于在选项卡窗格中为每个选项卡设置 fx:include 语句的答案。我已经轻松地设法让内容显示出来,但是无论我如何构造它,相关控制器类的引用方法只会给我一个空指针引用异常。包含的 FXML 布局的控制器既没有构造函数也没有 initialize() 方法,是否需要它们?我尝试了一些不同的方法,但总是遇到相同的异常。
我所做的只是在选项卡窗格中添加一个更改侦听器,当按下选项卡时,我想使用从全局可访问的数组列表中获取的一些值填充一些文本字段。注意: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>
上面的 FooTab 和 BarTab 的 onAction 都适用于它们各自的按钮。当这个方法(handleButton)是来自主控制器的引用时,那是我得到一个异常的时候。
【问题讨论】:
-
“无论我如何构造它,关联控制器类的引用方法只会给我一个空指针引用异常”。你能edit你的问题来展示你的尝试吗?
-
我已经更新了我目前拥有的测试代码,请指教
标签: java model-view-controller javafx fxml fxmlloader