简体   繁体   English

JavaFX UI创建和进度栏更新

[英]JavaFX UI creation & Progress bar Update

I have a rather tricky question here . 我这里有一个相当棘手的问题。

What I have : 我有的 :

I am currently making a JavaFX application that renders a graph ( with lots of nodes and edges ) on the screen . 我目前正在制作一个JavaFX应用程序,该应用程序在屏幕上呈现一个图形(具有许多节点和边缘)。 I am able to construct the graph using the Graphiz library and it shows fine on my screen . 我可以使用Graphiz库构造图形,并且在屏幕上可以正常显示。 But the graph rendering can take time bex my graph can contain >1000+ nodes and I drawing them on the screen is slow . 但是图形渲染可能会花费一些时间,因为我的图形可以包含> 1000+个节点,并且在屏幕上绘制它们的速度很慢。 Here is my code to do that 这是我的代码

protected synchronized void layoutWithGraphviz() {
    if (listener == null)
        listener = new LayoutResultListener();
    Utils.CreateGraphLayout(this,
            listener);
}

Now my LayoutResultListener() is a listener that ensures that the vertex on view dont overlap each other / look nice and hence I redraw the graph if they dont . 现在,我的LayoutResultListener()是一个侦听器,可确保视图上的顶点彼此不重叠/看起来不错,因此,如果不重叠,则可以重画图形。 This all works perfectly . 所有这一切都完美。

What I need : 我需要的 :

I want a progress bar to show my overall progress . 我想要一个进度条来显示我的整体进度。 Now since I am unsure how many times my LayoutResultLisener() will be called so i am OK with an indeterminate progress bar . 现在,由于我不确定我的LayoutResultLisener()将被调用多少次,所以我可以使用不确定的进度条来确定。 However the tricky part is I cant delegate the UI creation part to a background thread as JavaFX doesnt allow this . 但是,棘手的是我无法将UI创建部分委托给后台线程,因为JavaFX不允许这样做。 I see it documented here as well Concurrency in JavaFX 我在这里看到它以及JavaFX中的并发性

So i dont know what to do I cannot delegrate UI creation to background thread . 所以我不知道该怎么办,我不能将UI创建委托给后台线程。 So how do i show a progress bar ? 那么我如何显示进度栏? Any idea ? 任何想法 ?

Try to wrap your UI creation code inside : 尝试将您的UI创建代码包装在其中:

Platform.runLater(new Runnable(){
    @Override
    public void run() {
        //your JAVAFX UI codes
    }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM