简体   繁体   English

JavaFX应用程序:读取文件时在栏中显示进度

[英]JavaFX app: show progress in bar while reading file

I have a csv file reader, which calls a method that should update the progress bar. 我有一个csv文件阅读器,它调用一种应更新进度条的方法。 However the progress bar doesn't update, even when I try to schedule it with Platform.runLater. 但是,即使我尝试使用Platform.runLater安排进度条,进度条也不会更新。 The file reading is part of a synchronous processing so I can't just run the reader in its own thread - I have to wait for the thread to join. 文件读取是同步处理的一部分,因此我不能只在自己的线程中运行读取器-我必须等待线程加入。 The progressbar is working in some other context. 进度条在其他情况下也可以工作。 What I tried: 我试过的

public CsvFile(File file, SEPARATOR separator)
{
    this.file = file;
    this.separator = separator.value();
    setPattern();

    Thread thread = new Thread() {
        public void run() {
            readFile();
        };
    };
    thread.start();
    try
    {
        Thread.sleep(5000);//HACK
        thread.join();//HACK
    }
    catch (InterruptedException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

And the progress bar method, which is called by readFile(): 还有进度条方法,由readFile()调用:

public void setProgressExecute(double progress)

{
    //ProgressExecute.setProgress(progress);
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            ProgressExecute.setProgress(progress);
        }
    });
}

Update 更新

I am now calling new CsvFile in it own thread. 我现在在自己的线程中调用new CsvFile This makes the progressbar work. 这使进度条起作用。 However, it also forces me to use Platform.runLater to run GUI updates, since only the JavaFX thread may update certain parts of the GUI. 但是,这也迫使我使用Platform.runLater来运行GUI更新,因为只有JavaFX线程才能更新GUI的某些部分。 My new problem is with Platform.runLater: it runs stuff in LIFO order which reverses the order of processing. 我的新问题是Platform.runLater:它以LIFO顺序运行内容,从而颠倒处理顺序。 Well I guess you can't have it all. 好吧,我想你不可能拥有全部。

you can create a service class(LogFilesService extends Service) and bind the progressProperty. 您可以创建一个服务类(LogFilesService扩展Service)并绑定progressProperty。 At the Service class you can use the updateProgress to update the progress. 在Service类中,可以使用updateProgress更新进度。

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

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