JavaFx:parent.lookup返回null [英] JavaFx: parent.lookup returns null

查看:227
本文介绍了JavaFx:parent.lookup返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用osgi + cdi,并且具有以下代码:

I use osgi+cdi and I have the following code:

Parent parent=null;
FXMLLoader fxmlLoader=getFxmlLoader();
try {
    parent = (Parent)fxmlLoader.load(getFxmlStream("tasklist.fxml"));
} catch (IOException ex) {
    Logger.getLogger(TestGoView.class.getName()).log(Level.SEVERE, null, ex);
}
ComboBox comboBox=(ComboBox) parent.lookup("#testComboBox");
if (comboBox==null){
    System.out.println("COMBOBOX NULL");
}else{
    System.out.println("COMBOBOX NOT NULL");
}

我有以下tasklist.fxml

And I have the following tasklist.fxml

<VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="440.0" prefWidth="757.0" xmlns="http://javafx.com/javafx/8.0.60-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.techsenger.testgo.core.adm.task.list.TaskDirListController">
   <children>
      <HBox>
         <children>
            <ToolBar maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" nodeOrientation="RIGHT_TO_LEFT" HBox.hgrow="SOMETIMES">
               <items>
                  <ComboBox fx:id="testComboBox" maxWidth="1.7976931348623157E308" nodeOrientation="LEFT_TO_RIGHT" />
               </items>
            </ToolBar>
         </children>
      </HBox>
   </children>
</VBox>

但是parent.lookup(#testComboBox")返回null.怎么解释呢?我已经多次检查ID的名称.

However parent.lookup("#testComboBox") returns null. How to explain it? I've checked the name of ID several times.

推荐答案

您可以将所需的逻辑放在

Instead of using a lookup, which will only work after the scene has been rendered, you can put the logic you need in your controller class. You can inject elements from the FXML file into the controller class by annotating them @FXML.

public class TaskDirListController {

    @FXML
    private ComboBox<...> testComboBox ;

    public void initialize() {
        System.out.println(testComboBox);
    }

    // ...
}

查询通常不可靠,我建议避免使用它们.如果您确实需要从控制器以外的其他类访问FXML文件中定义的内容,则要做的第一件事就是考虑对内容进行重组,以使您无需这样做:这实际上表明您的总体设计是错误的.

Lookups are generally not robust, and I would recommend avoiding using them. If you really need to access something defined in the FXML file from a class other than the controller, the first thing to do is to consider reorganizing things so that you don't need to do this: it really indicates that your overall design is wrong.

如果由于某种原因确实需要此功能,最好使用FXMLLoadernamespace而不是查找:

If you really need this for some reason, it's better to use the FXMLLoader's namespace than a lookup:

Parent parent=null;
FXMLLoader fxmlLoader=getFxmlLoader();
try {
    parent = (Parent)fxmlLoader.load(getFxmlStream("tasklist.fxml"));
    ComboBox<?> comboBox = (ComboBox<?>) fxmlLoader.getNamespace().get("testComboBox");
    System.out.println(comboBox);
} catch (IOException ex) {
    Logger.getLogger(TestGoView.class.getName()).log(Level.SEVERE, null, ex);
}

这篇关于JavaFx:parent.lookup返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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