我希望获得有关在Tabpane中的每个选项卡上使用fx:include语句的一些答案。我已经设法轻松地获取内容以显示关联的控制器类的BUT引用方法,无论我如何构造它,都只是给了我一个nullpointerreference异常。包含的FXML布局的控制器既没有构造函数又没有initialize()方法,是否需要它们?我尝试了一些不同的操作,但总是遇到相同的异常。
我所做的只是在选项卡窗格中添加了一个更改侦听器,当按下选项卡时,我想用从全局可访问数组列表中获取的一些值填充某些文本字段。注意: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);
}
}
主控制器,我要在其中引用子控制器。
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>
BarTab:
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)是来自Main控制器的引用时,这就是我遇到异常的时候。
要为包含的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>
和
@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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句