问题
My javafx desktop application load and big text file during the initialization of the application in the
@Override
public void initialize(URL url, ResourceBundle rb) {
loadAppConfigurationFile();
}
private void loadAppConfigurationFile() {
Task<Void> task = new Task<Void>() {
@Override
public Void call() throws InterruptedException {
//read file and do some operation
return null;
}
};
new Thread(task).start();
}
But the problem here is my application GUI doesn't appear till this process it over. I invoked this file reading in another thread excepting that my GUI stage/scene will be loaded first and their I can show some loading message till that file is loaded in the system.
Kindly tell the exact workaround for this.
回答1:
i try this and its work easily...here bar
is a progress bar
@FXML
void initialize () {
assert bar != null : "fx:id=\"bar\" was not injected: check your FXML file 'Check.fxml'.";
loadAppConfigurationFile();
}
private void loadAppConfigurationFile () {
Task task = new Task<Void>() {
@Override
public Void call() throws InterruptedException {
int max = 1000000;
for (int i = 1; i <= max; i = i + 10) {
if (isCancelled()) {
break;
}
updateProgress(i, max);
System.out.println("somethings is here");
}
return null;
}
};
bar.progressProperty().bind(task.progressProperty());
new Thread(task).start();
}
来源:https://stackoverflow.com/questions/20091341/how-to-load-big-file-in-background-during-initialize-method-of-javafx