[英]Java FX - Progress bar update
I have a method that takes a while to complete, when the jar application is started. 当jar应用程序启动时,我有一种方法需要一段时间才能完成。 To have some feedback, i created the form
frmWaiting
, that displays a simple indeterminate progress bar. 为了获得一些反馈,我创建了
frmWaiting
表单,其中显示了一个简单的不确定进度栏。 I also have a controller for the form, PrincipalController
. 我也有一个窗体Controller,
PrincipalController
。
Entry point for the application 应用程序的入口点
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Stage stagePrincipal = new Stage();
Parent parentPrincipal = FXMLLoader.load(getClass().getClassLoader().getResource("frmPrincipal.fxml"));
Scene scenePrincipal = new Scene(parentPrincipal, 300, 275);
stagePrincipal.setScene(scenePrincipal);
stagePrincipal.setHeight(400);
stagePrincipal.setWidth(500);
stagePrincipal.setResizable(false);
stagePrincipal.setTitle("Instalador");
stagePrincipal.show();
} catch (Exception e) {
e.printStackTrace();
}
}
PrincipalController - frmPrincipal.fxml: PrincipalController-frmPrincipal.fxml:
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
Stage stageWaiting = new Stage();
Parent parentWaiting;
parentWaiting = FXMLLoader.load(getClass().getClassLoader().getResource("frmWaiting.fxml"));
Scene sceneWaiting = new Scene(parentWaiting, 300, 275);
stageWaiting.setScene(sceneWaiting);
stageWaiting.setHeight(300);
stageWaiting.setWidth(400);
stageWaiting.setResizable(false);
stageWaiting.setTitle("Instalador");
stageWaiting.show();
} catch (IOException e) {
}
}
WaitingController - frmWaiting.xml: WaitingController-frmWaiting.xml:
public class WaitingController implements Initializable {
@FXML private ImageView img;
@FXML private ProgressBar progressBar;
@FXML private ProgressIndicator pgIndicator;
private Task copyTask;
@Override
public void initialize(URL location, ResourceBundle resources) {
img.setImage(new Image(getClass().getClassLoader().getResourceAsStream("image.png")));
progressBar.setProgress(ProgressBar.INDETERMINATE_PROGRESS);
ArquivoController.getInstance().copiaArquivosPadrao(); //This is the method that takes a while.
}
public ProgressBar getProgressBar() {
return progressBar;
}
I want to initialize my main form, frmPrincipal
, when my method that takes a while finishes. 我想初始化我的主要形式
frmPrincipal
,这需要花一会儿时间。 I also want to get the progress bar working. 我也想让进度条正常工作。 I have tried to do it on another Thread, but i could not get the response from it when the method finishes.
我试图在另一个线程上执行此操作,但是方法完成后无法从中获取响应。
All the .fxml files are correct, ommited them to make things easier if possible. 所有.fxml文件都是正确的,如果可能的话,将其省略以使事情变得更容易。
The way it is, the application waits for the method to finish, then opens the other form. 这样,应用程序等待方法完成,然后打开其他表单。 But, the progressBar does not update.
但是,progressBar不会更新。
Inside ArquivoController.getInstance().copiaArquivosPadrao();
内部
ArquivoController.getInstance().copiaArquivosPadrao();
you have to update the progressBar
's progress. 您必须更新
progressBar
的进度。
You could do it the nice way, using a Task
to run copiaArquivosPadrao()
, update the tasks progress accordingly and binding to the tasks progress property. 您可以通过使用
Task
来运行copiaArquivosPadrao()
,并相应地更新任务进度并将其绑定到task progress属性,这是一种很好的方法。
Or you could do it the ugly way, passing progressBar
to copiaArquivosPadrao()
, something like this: 或者,您可以通过将
progressBar
传递给copiaArquivosPadrao()
来copiaArquivosPadrao()
以下操作:
public void initialize(URL location, ResourceBundle resources) {
img.setImage(new Image(getClass().getClassLoader().getResourceAsStream("image.png")));
progressBar.setProgress(ProgressBar.INDETERMINATE_PROGRESS);
ArquivoController.getInstance().copiaArquivosPadrao(progressBar); //This is the method that takes a while.
}
and 和
public void copiaArquivosPadrao(ProgressBar progressBar) {
// call progressBar.setProgress() in here to update the progress bar
// eg.
progressBar.setProgress(0.0F);
doSomething();
progressBar.setProgress(0.20F);
doSomething();
progressBar.setProgress(0.40F);
doSomething();
progressBar.setProgress(0.60F);
doSomething();
progressBar.setProgress(0.80F);
doSomething();
progressBar.setProgress(1.00F);
}
For sure, you can do this more fine-grained in a loop or similar. 当然,您可以通过循环或类似方式更细粒度地执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.