JavaFX - 从嵌套的FXML访问fx:id [英] JavaFX - Access fx:id from nested FXML

查看:1329
本文介绍了JavaFX - 从嵌套的FXML访问fx:id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这是我的主要FXML文件,名为'Home.fxml':

So, this is my main FXML file, called 'Home.fxml':

<VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
    <fx:include source="MenuBar.fxml" />
   <Label alignment="CENTER" maxWidth="1.7976931348623157E308" text="Welcome to MSMusic" textAlignment="CENTER">
      <font>
         <Font size="62.0" />
      </font>
   </Label>
    <fx:include source="PlayerElement.fxml" />
</VBox>

在该文件中我包含一个音乐播放器元素,其标签带有fx:id'songTime ',当我尝试在Home.fxml的Controller中使用'songTime'时,我得到一个NullPointerException,因为嵌套fxml中的fx:id似乎不可用。有没有一种简单的方法来实现这一目标?

within that file i am including a music player element which has a label with the fx:id 'songTime', when i try to use 'songTime' within the Controller of Home.fxml i get a NullPointerException, because the fx:id from within a nested fxml doesn't seem to be useable. Is there a easy way to achieve this?

推荐答案

在FXML的控制器外部公开UI控件通常是不好的做法它们出现的文件。

It is generally bad practice to expose UI controls outside of the controller for the FXML file in which they occur.

您可以将控制器从包含的FXML文件注入控制器,用于 Home.fxml file:

You can inject the controller from the included FXML file into the controller for your Home.fxml file:

<VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
    <fx:include source="MenuBar.fxml" />
   <Label alignment="CENTER" maxWidth="1.7976931348623157E308" text="Welcome to MSMusic" textAlignment="CENTER">
      <font>
         <Font size="62.0" />
      </font>
   </Label>
    <fx:include fx:id="player" source="PlayerElement.fxml" />
</VBox>

和控制器 Home.fxml 你可以做到

public class HomeController {

    @FXML
    private PlayerElementController playerController ;

    // ...
}

其中 PlayerElementController PlayerElement.fxml 文件的控制器类。这在嵌套中有所描述。控制器在文档中,但基本上只使用名称为 fx:id 的字段为 fx:include 附加Controller,所以这里 fx:id =player for include允许您将包含的FXML文件的控制器实例注入字段 playerController

where PlayerElementController is the controller class for the PlayerElement.fxml file. This is described under "Nested Controllers" in the documentation, but essentially just use a field whose name is the fx:id for the fx:include with "Controller" appended to it, so here fx:id="player" for the include allows you to inject the controller instance for the included FXML file to the field playerController.

现在只需定义一些方法在 PlayerElementController 中设置所需的文本:

Now just define some methods in PlayerElementController for setting the desired text:

public class PlayerElementController {

    @FXML
    private Label songTime ;

    // note: might want to make the parameter a more appropriate type than string,
    // and perform the conversion to a string in this method...
    public void setSongTime(String songTime) {
        this.songTime.setText(songTime);
    }

    // and similarly here for the return type
    public String getSongTime() {
        return songTime.getText();
    }

    // ...
}

现在回到你的 HomeController 你需要做的就是

Now back in your HomeController all you need do is

playerController.setSongTime(...);

设置文本。如果您需要与标签相关的其他功能,只需定义与您需要的行为相对应的适当方法。

to set the text. If you need other functionality associated with the label, just define the appropriate methods corresponding to the behavior you need.

这篇关于JavaFX - 从嵌套的FXML访问fx:id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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