JavaFX Node.lookup() 仅对使用 FXMLLoader 加载的 Parent 中的某些元素返回 null

JavaFX Node.lookup() returning null only for some elements in Parent loaded with FXMLLoader

我从 FXML 加载了一个 Parent,将它添加到 scene/stage 内的一个 Pane 并显示了它,然后我立即查找组件。 lookup() returns 对某些人为 null 但对其他人为非 null。它在什么情况下会这样做?

加载和查找代码如下:

    rootUi = FXMLLoader.load(getClass().getResource("PracticeScreen.fxml"));
    // added to Parent within stage and setVisible(true)
    analysisGroup = (Pane)rootUi.lookup("#analysisGroup");       // null
    stickiesPane = (Pane)rootUi.lookup("#stickiesPane");         // null
    scoreScroll = (ScrollPane)rootUi.lookup("#scoreScrollPane"); // GOOD
    tintLayer = rootUi.lookup("#tintLayer");                     // GOOD
    scoreImageView = (ImageView)rootUi.lookup("#scoreImage");    // null
    StackPane scoreRenderStack = (StackPane)rootUi.lookup("#scoreRenderStack"); // null
    StackPane scoreScrollStack = (StackPane)rootUi.lookup("#scoreScrollStack"); // null

scoreScrollPane 正常返回,但随后它的所有子项都返回 null。

已加载 FXML:

  <?import javafx.geometry.Insets?>
  <?import javafx.scene.control.Label?>
  <?import javafx.scene.control.ScrollPane?>
  <?import javafx.scene.control.ToggleButton?>
  <?import javafx.scene.effect.DropShadow?>
  <?import javafx.scene.effect.Glow?>
  <?import javafx.scene.image.ImageView?>
  <?import javafx.scene.layout.*?>
  <?import javafx.scene.shape.Rectangle?>
  <?import javafx.scene.text.Font?>
  <BorderPane style="-fx-background-color: white;" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
     <center>
        <StackPane id="scoreScreenStack" style="-fx-background-color: blue;" BorderPane.alignment="CENTER">
           <children>
              <ScrollPane id="scoreScrollPane" fitToHeight="true" hbarPolicy="ALWAYS" vbarPolicy="NEVER">
                 <content>
                    <StackPane id="scoreScrollStack">
                       <children>
                          <StackPane id="scoreRenderStack" alignment="CENTER_LEFT" StackPane.alignment="CENTER_LEFT">
                             <children>
                                <ImageView id="scoreImage" pickOnBounds="true" preserveRatio="true" StackPane.alignment="CENTER_LEFT" />
                                <Pane id="analysisGroup" />
                                <Pane id="stickiesPane" minHeight="200.0" minWidth="200.0" />
                                <Rectangle id="playbackCursor" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="150.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" visible="false" width="2.0">
                                   <effect>
                                      <DropShadow color="DODGERBLUE" />
                                   </effect>
                                </Rectangle>
                             </children>
                          </StackPane>
                       </children>
                    </StackPane>
                 </content>
              </ScrollPane>
              <BorderPane pickOnBounds="false">
                 <top>
                    <StackPane BorderPane.alignment="CENTER">
                       <children>
                          <BorderPane>
                             <left>
                                <Label id="recordingIndicator" text="Listening..." textFill="#00a7ff" BorderPane.alignment="CENTER">
                                   <font>
                                      <Font size="18.0" />
                                   </font>
                                   <effect>
                                      <Glow level="0.86" />
                                   </effect>
                                   <BorderPane.margin>
                                      <Insets />
                                   </BorderPane.margin>
                                   <padding>
                                      <Insets bottom="8.0" left="8.0" right="8.0" top="8.0" />
                                   </padding>
                                </Label>
                             </left>
                             <right>
                                <HBox BorderPane.alignment="CENTER">
                                   <children>
                                      <ToggleButton id="finalPerformance" mnemonicParsing="false" text="Final Performance" />
                                   </children>
                                   <BorderPane.margin>
                                      <Insets bottom="8.0" left="8.0" right="8.0" top="8.0" />
                                   </BorderPane.margin>
                                </HBox>
                             </right>
                          </BorderPane>
                       </children>
                    </StackPane>
                 </top>
              </BorderPane>
              <Pane id="tintLayer" opacity="0.0" pickOnBounds="false" style="-fx-background-color: #00a7ff;" visible="false" />
           </children>
        </StackPane>
     </center>
  </BorderPane>

这是因为 lookup() 只查看子项,而不是 "content" scoreScrollPane 的情况,它是一个 ScrollPane。 ScrollPane 的内容不包含在其子项中。此外,rootUi.getScene().lookup("#scoreScrollPane") returns 也为空。你必须改为:

scoreImageView = (ImageView)scoreScroll.getContent().lookup("#scoreImage");    // GOOD now

在 CSS 应用于场景之前,无法保证查找有效。这通常发生在第一次布局传递时(即第一次将场景图渲染到屏幕上),在某些情况下甚至可能晚于此发生。通常不推荐将查找作为获取您在场景图中定义的节点的机制。

引用 FXML 中定义的控件的推荐方法是使用控制器 class。将 FXML 中的所有 id 属性更改为 fx:id 属性,并将控制器 class 属性添加到根元素:

<BorderPane style="-fx-background-color: white;" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="com.my.company.PracticeScreenController">
     <center>
        <StackPane fx:id="scoreScreenStack" style="-fx-background-color: blue;" BorderPane.alignment="CENTER">
           <children>
              <ScrollPane fx:id="scoreScrollPane" fitToHeight="true" hbarPolicy="ALWAYS" vbarPolicy="NEVER">
                 <content>
                    <StackPane fx:id="scoreScrollStack">

                        <!-- etc -->

                    </StackPane>
                 </content>
              </ScrollPane>
           </children>
        </StackPane>
    </center>
</BorderPane>

现在用相应的 @FXML 注释字段定义一个控制器 class。您可以在 initialize() 方法中访问这些字段(请注意,在调用构造函数时它们不会被初始化):

package com.my.company ;

// imports...

public class PracticeScreenController {

    @FXML
    private StackPane scoreScreenStack ;
    @FXML
    private ScrollPane scoreScrollPane ;
    @FXML
    private ScrollPane scoreScrollStack ;

    // etc...

    public void initialize() {
        // configure controls here, as needed...
    }
}

如果迫切需要从加载 FXML 文件的位置(而不是在控制器中)检索对 FXML 文件中定义的节点的引用,您可以通过 FXMLLoader's命名空间。再次强调,这 不是推荐的方法 ,您应该真正使用控制器 class 来访问这些,但这确实提供了另一种解决方案。

使用上面 FXML 中显示的相同 fx:id 属性,您可以:

FXMLLoader loader =  new FXMLLoader(getClass().getResource("PracticeScreen.fxml"));
rootUi = loader.load();

Map<String, Object> namespace = loader.getNamespace();

// added to Parent within stage and setVisible(true)
analysisGroup = (Pane)namespace.get("analysisGroup");
stickiesPane = (Pane)namespace.get("stickiesPane");
scoreScroll = (ScrollPane)namespace.get("scoreScrollPane"); 
tintLayer = (Pane)namespace.get("tintLayer");                     
scoreImageView = (ImageView)namespace.get("scoreImage");
StackPane scoreRenderStack = (StackPane)namespace.get("scoreRenderStack");
StackPane scoreScrollStack = (StackPane)namespace.get("scoreScrollStack");