0
是否可以在一个进度条中呈现两个嵌套任务的进度? 我的意思是,如果我必须从几个目录中的几个文件读取行。我有这样的代码片段:将进度任务嵌套到一个进度条中javafx
编辑:
final int steps = directories.size();
Task<Integer> task2 = new Task<Integer>() {
/**
*/
@Override
protected Integer call() throws Exception {
int len = files.length;
updateProgress(0, len);
for(int i = 0; i < files.length; i++) {
final String filename = files[i].getName();
final String file = files[i].getPath();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
}
String currentLine = null;
while ((currentLine = br.readLine()) != null) {
readlines(currentLine, filename);
}
} catch (IOException xs) {
xs.getMessage();
}
System.out.println("******* Working on: " + file + " ******************************");
updateProgress(i + 1, files.length);
}
System.out.println("done: " + directoryName);
return len;
}
};
task2.setOnRunning(r -> {
DoubleBinding totalProgress = null;
for(int i = 0; i < steps; i++) {
DoubleBinding increment = task2.progressProperty().divide(steps);
if (totalProgress == null) {
totalProgress = increment;
} else {
totalProgress = totalProgress.add(increment);
}
}
progressBar.progressProperty().bind(totalProgress);
status.textProperty().bind(task2.stateProperty().asString());
statusProcess.textProperty().bind(Bindings.when(totalProgress.isEqualTo(-1))
.then("0.00")
.otherwise(task2.progressProperty().multiply(100.00).asString("%.02f %%")));
console.textProperty().bind(task2.messageProperty());
});
new Thread(task2).start();
我能够访问进度条,但只要我有目录,进度条会重新启动为0,将检查文件到下一个目录,然后填充进度栏直到1,然后如果有另一个目录,则重新启动到0。
如果我想在一个显示这两个任务,我不想从下一个目录正在读取时从0重新启动,我该怎么办?如果我有例如4个目录,我希望看到一个移动进度条直到其四分之一等等。
有人认为我做错了什么吗?
感谢您的回答,但这不适合我。如果我按照您的建议进行操作,progressBar将显示一个从50%开始的蓝色条,并且在以60%左右的速度更改目录时重新启动100%以重新启动... :( – cy8el1us
@ cy8el1us啊,好的,我没有读到您的为什么在第二个任务中重复执行进度为零?通常,在这里总共迭代多少个文件? –
没有pb!我减少了代码,现在只使用一个任务,但现在,当任务访问读取文件的方法时,访问似乎没有同步,所以结果是每个文件的混合。你对如何解决这个问题有一个想法吗?看我的编辑 – cy8el1us