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