此代码用于进度条。我将此进度条用于后台进程,但问题是进度条仅在后台任务完成后可见!!
public class ProgBar {
public void porgressBarTest(){
ProgressBar progressBar = new ProgressBar();
progressBar.setProgress(0);
progressBar.setMinWidth(400);
VBox updatePane = new VBox();
updatePane.setPadding(new Insets(30));
updatePane.setSpacing(5.0d);
updatePane.getChildren().addAll(progressBar);
Stage taskUpdateStage = new Stage(StageStyle.UTILITY);
taskUpdateStage.setScene(new Scene(updatePane));
taskUpdateStage.show();
Task<Void> task = new Task<Void>() {
public Void call() throws Exception {
int max = 200;
for (int i = 1; i <= max; i++) {
updateProgress(i, max);
Thread.sleep(100);
}
System.out.println("about to close");
return null;
}
};
progressBar.progressProperty().bind(task.progressProperty());
new Thread(task).start();
}
}
我想为这个方法使用进度条!
public void exportFile(String fileFormat) throws EngineException {
String output = *************;
String reportDesignFilePath = ********************;
// Save Report In Particular Format
try {
EngineConfig configure = new EngineConfig();
Platform.startup(configure);
IReportEngineFactory reportEngineFactory=(IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
IReportEngine engine = reportEngineFactory.createReportEngine(configure);
engine.changeLogLevel(Level.WARNING);
IReportRunnable runnable = engine.openReportDesign(reportDesignFilePath);
IRunAndRenderTask task = engine.createRunAndRenderTask(runnable);
IRenderOption option = new PDFRenderOption();
option.setOutputFormat(fileFormat);
option.setOutputFileName(output+fileFormat);
task.setRenderOption(option);
task.run();
task.close();
}
catch(Exception e) { e.printStackTrace(); }
// Open Created File
File file = new File(output+fileFormat);
if (file.exists()) {
if (Desktop.isDesktopSupported()) {
try {
Desktop desktop = Desktop.getDesktop();
desktop.open(file);
}
catch(IOException e) {
e.printStackTrace();
}
}
}
}
我认为您正在尝试在 progressBar 加载完成后调用 exportFile() 方法。
你的代码看起来不错。要在加载 progressBar 后调用 exportFile() 方法,您可以调用 setOnSuccededMethod ,如下所示。
Task<Void> task = new Task<Void>() {
public Void call() throws Exception {
int max = 200;
for (int i = 1; i <= max; i++) {
updateProgress(i, max);
Thread.sleep(100);
}
System.out.println("about to close");
return null;
}
};
progressBar.progressProperty().bind(task.progressProperty());
task.setOnSucceeded(e -> {
exportFile();
});
task.setOnFailed(e -> {
//more things
});
new Thread(task).start();
这样,当进度条加载完成时,exportFile() 函数将被调用。该程序可能仍会挂起,因为您在 exportFile() 方法中使用了 Desktop 类。将 JAVAFX 线程与 Desktop 类结合使用时,它无法正常工作。JavaFX 线程有点被阻塞。您将需要创建一个新线程来打开导出的文件,如下所示。
public void exportFile(String fileFormat) throws Exception{
//upper portion of Code
File fileToOpen = new File("LocationOfFile");
if(Desktop.isDesktopSupported()) {
new Thread(() -> {
try {
Desktop.getDesktop().open(fileToOpen);
System.out.println("FileOpened Successfully");
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
System.out.println("exportFile finishing");
}
链接问题:JavaFX 线程问题
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句