javafx 在方法完成之前不会重新绘制标签,为什么?

发布于 2024-08-31 03:27:36 字数 1313 浏览 2 评论 0原文

我有一个JavaFX应用程序,其中有一些像这样的代码...

public class MainListener extends EventListener{
    override public function event (arg0 : String) : Void {
     statusText.content = arg0;
    }
}

statusText是这样定义的...

    var statusText = Text {
    x: 30
    y: stageHeight - 40
    font: Font { name: "Bitstream Vera Sans Bold" size: 10 }
    wrappingWidth: 420
    fill: Color.WHITE
    textAlignment: TextAlignment.CENTER
    content: "Status: awaiting DBF file."
};

我还有一些其他加载数据的Java代码,很像这样..

public ArrayList<CustomerRecord> read(EventListener listener) {

    ArrayList<CustomerRecord> listOfCustomerRecords = new ArrayList<CustomerRecord>();
        listener.event("Status: Starting read");

        // ** takes a while...
        List<Map<String, CustomerField>> customerRecords = new Reader(file).readData(listener);
        // ** long running method over.

        listener.event("Status: Loaded all customers, count:" + listOfCustomerRecords.size());
    return listOfCustomerRecords;
}

现在,虽然最后一个方法处于长时间运行的调用中,我希望看到我的 statusText 更新为“状态:开始阅读”,但事实并非如此。仅当 read() 方法返回时文本才会更新。

如果它是“直接”java,我会假设长时间运行的作业正在占用 CPU,或者 statusText 需要调用 repaint() 。

谁能给我任何想法吗?

谢谢 杰夫·波特

I have a JavaFX app with a some code like this...

public class MainListener extends EventListener{
    override public function event (arg0 : String) : Void {
     statusText.content = arg0;
    }
}

statusText is defined like this...

    var statusText = Text {
    x: 30
    y: stageHeight - 40
    font: Font { name: "Bitstream Vera Sans Bold" size: 10 }
    wrappingWidth: 420
    fill: Color.WHITE
    textAlignment: TextAlignment.CENTER
    content: "Status: awaiting DBF file."
};

I also have some other Javacode that is load data, much like this..

public ArrayList<CustomerRecord> read(EventListener listener) {

    ArrayList<CustomerRecord> listOfCustomerRecords = new ArrayList<CustomerRecord>();
        listener.event("Status: Starting read");

        // ** takes a while...
        List<Map<String, CustomerField>> customerRecords = new Reader(file).readData(listener);
        // ** long running method over.

        listener.event("Status: Loaded all customers, count:" + listOfCustomerRecords.size());
    return listOfCustomerRecords;
}

Now while the last method is in its long running call, I would expect to see my statusText updated to have 'Status: Starting read', but its doesn't. Its only when the read() method returns that the text is updated.

If its was 'straight' java I would presume that the long running job is hogging the CPU, or the statusText needed to have repaint() called on it.

Can anyone give me any ideas?

Thanks
Jeff Porter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒗幽 2024-09-07 03:27:36

JavaFX 是单线程的,因此您在主线程上执行的任何操作都会阻止重绘。如果您想在方法运行期间进行重绘,则需要在单独的线程上运行。例如,您可以使用 javafx.async.JavaTaskBase 来实现此目的。

JavaFX is single-threaded so anything you do on the main thread will block repaints. If you want to have repaints during a method run you need to run in on a separate thread. You can for example use javafx.async.JavaTaskBase for that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文