JavaFX TitledPane查找(.title)返回null [英] JavaFX TitledPane lookup(.title) returns null

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

问题描述

我是Java FX的新手,正在创建一个有趣的应用程序。我正在尝试动态添加 TitledPane 并在尝试查找 TitledPane 的标题时获取Null Pointer Exceptions大约70%的时间。我尝试创建一个简单的演示我的问题,但无法重现我的应用程序之外的问题,但我可以解决我的问题。我希望有人能帮助我理解为什么我的解决方案有效,并且可能会指出我更好的解决方案。我正在使用带控制器的FXML文件。我正在尝试在Platform.runLater()中查找标题,因为我手动编辑标题的布局和元素。在Controller的初始化函数内部,我执行以下操作来获取空指针异常:

I'm new to Java FX and am creating an application for fun. I'm trying to add a TitledPane dynamically and am getting Null Pointer Exceptions when attempting to lookup the title of the TitledPane about 70% of the time. I tried to create a simple demo of my issue, but was unable to reproduce the issue outside of my application, but I could solve my issue. I'm hoping someone could help me understand why my solution works and maybe point me in the direction of a better solution. I'm using an FXML file with a Controller. I'm attempting to lookup the title inside of Platform.runLater() because I'm manually editing the layout and elements of the title. Inside of the Controller's initialize function, I do the following to get null pointer exceptions:

Platform.runLater(new Runnable() {
    @Override
    public void run() {
        titledpane.lookup(".title"); // will return null about 70% of the time
    }
});
// Do more stuff

但是,如果我将这个调用包装在一个定时器中并执行它在500毫秒内,似乎永远不会返回Null。

However, if I wrap that call in a timer and execute it in 500 ms, it doesn't seem to ever return Null.

new java.util.Timer().schedule(new java.util.TimerTask() {
    @Override
    public void run() {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                titledpane.lookup(".title"); // doesn't seem to return null
            }
        });
    }
}, 500);

一个论坛提到在调用标题上的查询之前必须将CSS应用于元素,所以我尝试手动将CSS应用于标题,但 titledpane.lookup(。title)仍然返回null。任何人都可以帮我理解这里发生的事情吗?提前致谢!

One forum mentioned that CSS had to be applied to the element prior to calling a lookup on the title, so I tried manually applying CSS to the title, but titledpane.lookup(".title") still returned null. Can anyone help me understand what is happening here? Thanks in advance!

推荐答案

我遇到了同样的问题。我通过在包含的窗格上调用 applyCss() layout()来解决它TitledPane

I had the same issue. I resolved it by calling applyCss() and layout() on the pane that contains the TitledPane:

Node loadedPane = paneLoader.load();
bodyPane.setCenter(loadedPane);
bodyPane.applyCss();
bodyPane.layout();

这篇关于JavaFX TitledPane查找(.title)返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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