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

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

问题描述

我是 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 字段 sample.Controller.mConfigTabPage 到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 附加后缀 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:每个选项卡一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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