JavaFx TabPane:每个Tab一个控制器 [英] JavaFx TabPane : One controller per Tab

查看:717
本文介绍了JavaFx TabPane:每个Tab一个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javafx的新手,我正试图在每个标签页上放置一个控制器。
我找到了这个答案: https://stackoverflow.com/a/19889980/393984 导致我这样做:

I'm new with javafx and i'm trying to have one controller per tab in a tabpane. I found this answer : https://stackoverflow.com/a/19889980/393984 which lead me to do this :

Main.fxml

<TabPane fx:controller="sample.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/null">
  <tabs>
    <Tab text="Configuration">
      <content>
        <fx:include fx:id="mConfigTabPage" source="configTab.fxml"/>
      </content>
    </Tab>
    <Tab text="TODO">
      <content>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
      </content>
    </Tab>
  </tabs>
</TabPane>

configTab.fxml

<Pane fx:controller="sample.ConfigController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/null" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label layoutX="23.0" layoutY="22.0" text="API Key :" />
      <TextField layoutX="95.0" layoutY="18.0" fx:id="mAPIKey" />
   </children>
</Pane>

Controller.java

public class Controller {
    private Stage mStage;

    @FXML
    private ConfigController mConfigTabPage;

    public void Controller(){}

    public void setStage(Stage stage)
    {
        mStage = stage;
    }

    @FXML
    public void initialize() {
        System.out.println("CONTROLLER");
    }
}

ConfigController.java

public class ConfigController {
    public void ConfigController(){}

    @FXML
    public void initialize() {
        System.out.println("CONFIG CONTROLLER");
    }
}

如果我删除我的程序正在启动

My program is launching if i remove

@FXML
private ConfigController mConfigTabPage;

但是一旦我添加它我有以下例外:

But as soon as i add it i have the following exception :


java.lang.IllegalArgumentException:无法设置
sample.ConfigController field sample.Controller.mConfigTabPage to
javafx.scene.layout.AnchorPane

java.lang.IllegalArgumentException: Can not set sample.ConfigController field sample.Controller.mConfigTabPage to javafx.scene.layout.AnchorPane

所以我猜javafx试图将我的控制器转换为AnchorPane并导致问题。

So i'm guessing that javafx trying to cast my controller into an AnchorPane and that causing the problem.

我应该怎样做才能在我的主控制器中引用每个窗格的控制器?

What should i do to be able to have a reference of each pane's controllers in my main controller ?

推荐答案

如果你想要控制器的东西 fx:id =something 将后缀 Controller 附加到Java成员字段。所以你必须使用:

If you want the controller of something with fx:id="something" append the suffix Controller to your Java member field. So you have to use:

@FXML
private ConfigController mConfigTabPageController;

参见参考

这篇关于JavaFx TabPane:每个Tab一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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