JavaFX Node.lookup()仅为使用FXMLLoader

时间:2016-04-21 12:38:10

标签: javafx fxml

我从FXML加载了一个Parent,将它添加到场景/舞台中的一个窗格并显示它,然后立即查找组件。 lookup()为某些人返回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返回OK,但随后它的所有子节点都返回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>

2 个答案:

答案 0 :(得分:5)

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

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

<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 - 注释字段的控制器类。您可以在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&#39;来执行此操作。命名空间。同样,我应该强调这是不是推荐的方法,你应该使用控制器类来访问它们,但这确实提供了另一种解决方案。

使用上面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");

答案 1 :(得分:1)

这是因为lookup()仅查看子项,而不是像ScoreScrollPane(ScrollPane)一样的“内容”。 ScrollPane的内容未包含在其子级中。此外,rootUi.getScene().lookup("#scoreScrollPane")也返回null。你必须改为:

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