I know, that this Question has been answered more than enough, but not even one solution fit my Problem.
Case:
I want to import 1:n files into a database. So far so good. for each file, the ProgressIndicator should reset the Progress Value. Yep, it does. While importing my stuff into a database, the GUI seems to be "stuck" till the next file import starts.
My Problem:
The Value resets, but the ProgressIndicator don't.
But the Value is updating correctly.
System.out.println(progressIndicator.getProgress());
shows the updated results til it reached 1.0.
Until the import is finished, it jumps straight to 100% and never reset.. through there are more imports following and the progress is updating constantly with the right values. After the Progress reached 1.0 = 100% the next Import starts. Where is my mistake?
ProgressUpdater class:
public void activateBind(ReadOnlyDoubleProperty binding) {
if (pg != null) {
pg.progressProperty().unbind();
pg.progressProperty().bind(binding);
}
}
Notice, that all used Classes get initialized for each file import. PS: yes, I spelled "ProgressIndicator" wrong in my code.
Controller class:
@FXML
ProgressIndicator progessIndicator;
ImportTask task = new ImportTask(dbConnector, insertStatements, this);
final ProgressUpdater pgup = new ProgressUpdater(progessIndicator, dbConnector.getPercentVal());
pgup.activateBind(task.progressProperty());
new ImportThread(task, counter);
Import Task class:
@Override
protected Void call() throws Exception {
for (int i = 0; i < insertStatements.size(); i++) {
dbC.executeInsert(insertStatements.get(i));
updateProgress(dbC.getCurrent(), dbC.getEnd());
}
mc.executeImport();
return null;
}
The ImportThread class extends from Thread:
public ImportThread(Task task, int d) {
super(task, "Import " + d);
setDaemon(true);
Platform.runLater(new Runnable() {
@Override
public void run() {
start();
try {
join();
} catch (InterruptedException e) {
System.out.println("JOIN ERROR");
e.printStackTrace();
}
}
});
}
Thanks in advance for any help.
You are, for some reason, deliberately forcing the UI thread to wait for your background task to complete, by wrapping a call to join()
in a call to Platform.runLater()
. Consequently the UI thread can't do anything else (such as render your progress indicator) until the task is finished.
If you want the thread to start immediately on creation (which is not that good an idea, tbh), just call start()
in the constructor:
public ImportThread(Task task, int d) {
super(task, "Import " + d);
setDaemon(true);
start();
}
There is no obvious reason Thread.start()
needs to be executed on the FX application thread, so there is no need for a Platform.runLater()
here.