JavaFX 8-选项卡和选项卡,每个选项卡具有单独的FXML和控制器 [英] JavaFX 8 - Tabpanes and tabs with separate FXML and controllers for each tab

查看:206
本文介绍了JavaFX 8-选项卡和选项卡,每个选项卡具有单独的FXML和控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望获得有关在选项卡窗格中的每个选项卡具有fx:include语句的一些答案.我已经设法轻松地获取内容以显示关联的控制器类的BUT引用方法,无论我如何构造它,都只是给了我一个nullpointerreference异常.包含的FXML布局的控制器既没有构造函数又没有initialize()方法,是否需要它们?我尝试了一些不同的操作,但是总是遇到相同的异常.

I hope to get some answers regarding having fx:include statements for each tab in a tabpane. I have managed with ease to get content to show up BUT referencing methods of the associated controller class simply gives me a nullpointerreference exception no matter how I structure it. The controllers of the included FXML layouts do not have neither constructor not initialize() methods, are they needed? I tried some different things but always got the same exception.

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

What I simply did was add a change listener to the tabpane and when a tab was pressed I wanted to populate some textfields with some values gotten from a globally accessible arraylist. Note: the arraylist is not the issue, performing this operation using the main controller works fine.

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

I'm going to add a code example shortly but cannot right now. Please let me know if you need more info, otherwise I'll post the code later today.

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

*Edit, here is my code example, taken from another thread here on StackOverflow. JavaFX TabPane - One controller for each tab

TestApp.java:

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

主控制器,我要在其中引用子控制器.

Main controller, where I want to reference the sub controllers.

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:

FooTab's .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

BarTab's .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控制器的引用时,那就是我遇到异常的情况.

The above onAction for both FooTab and BarTab works with their respective buttons. When this method (handleButton) is references from the Main controller, that's when I get an exception.

推荐答案

要为包含的FMXL文件注入控制器,您需要在<fx:include>元素上具有fx:id属性.控制器将被注入到在fx:id值后附加"Controller"的字段.

To inject a controller for an included FMXL file, you need an fx:id attribute on the <fx:include> element. The controller will be injected to a field with "Controller" appended to the fx:id value.

如果您也想注入实际的Tab,则需要单独的fx:id.

If you want to inject the actual Tab too, you need a separate fx:id for that.

所以:

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

这篇关于JavaFX 8-选项卡和选项卡,每个选项卡具有单独的FXML和控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆