我将几个JavaFx Panel嵌入到Swing应用程序中,这些也很好地运行。现在我想在启动插件时添加一个进度条,这是我的问题。进度条显得很晚。
这是我用于嵌入的类。
private ProgressIndicator indicator;
private Stage stage;
private Scene scene;
private BorderPane root;
public FxIOGuiProgressTask() {
initGui();
startJfxProgressTask();
}
private void startJfxProgressTask() {
Task task = new Task<Void>() {
@Override
public Void call() {
try {
load();
} catch (Exception ex) {
logException(ex);
return null;
}
Platform.runLater(() -> {
try {
finished();
stage.close();
afterFinishedInformation();
} catch (Exception ex) {
logException(ex);
}
});
return null;
}
};
new Thread(task).start();
}
protected abstract void load() throws Exception;
protected abstract void finished() throws Exception;
public void afterFinishedInformation() {
}
private void initGui() {
Platform.runLater(() -> {
indicator = new ProgressIndicator();
indicator.setMaxSize(150, 150);
stage = new Stage();
stage.setTitle("Wird geladen...");
stage.initStyle(StageStyle.TRANSPARENT);
stage.initModality(Modality.APPLICATION_MODAL);
root = new BorderPane(indicator);
root.setStyle("-fx-background-color: transparent");
scene = new Scene(root, Color.TRANSPARENT);
stage.setAlwaysOnTop(true);
stage.setScene(scene);
stage.show();
});
}
如果我将finished()方法移出Platform.runLater(),并将其放在load()方法之后,则会发生“不在FX应用程序线程上”的异常。
也许有人知道答案。