简体   繁体   English

JavaFX-从嵌套FXML访问fx:id

[英]JavaFX - Access fx:id from nested FXML

So, this is my main FXML file, called 'Home.fxml': 因此,这是我的主要FXML文件,称为“ 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>

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. 在该文件中,我包括一个音乐播放器元素,该元素的标签带有fx:id'songTime',当我尝试在Home.fxml的Controller中使用'songTime'时,我得到了NullPointerException,因为fx:id来自内部嵌套的fxml似乎不可用。 Is there a easy way to achieve this? 有没有简单的方法可以做到这一点?

It is generally bad practice to expose UI controls outside of the controller for the FXML file in which they occur. 通常,对于在其中出现UI的FXML文件,将UI控件公开在控制器之外是不正确的做法。

You can inject the controller from the included FXML file into the controller for your Home.fxml file: 您可以将控制器从随附的FXML文件注入到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 fx:id="player" source="PlayerElement.fxml" />
</VBox>

and in the controller for Home.fxml you can do Home.fxml的控制器中,您可以执行

public class HomeController {

    @FXML
    private PlayerElementController playerController ;

    // ...
}

where PlayerElementController is the controller class for the PlayerElement.fxml file. 其中, PlayerElementControllerPlayerElement.fxml文件的控制器类。 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 . 在文档的“嵌套控制器”下进行了描述 ,但实际上仅使用名称为fx:id的字段作为fx:include并附加了"Controller" ,因此此处的fx:id="player"包括允许您将包含的FXML文件的控制器实例注入到字段playerController

Now just define some methods in PlayerElementController for setting the desired text: 现在,只需在PlayerElementController定义一些用于设置所需文本的方法即可:

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

    // ...
}

Now back in your HomeController all you need do is 现在回到您的HomeController您需要做的就是

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. 如果需要与标签关联的其他功能,只需定义与所需行为对应的适当方法即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM