Java FX - 进度条更新

时间:2017-07-24 21:55:00

标签: java javafx javafx-8

我有一个方法需要一段时间才能完成,当jar应用程序启动时。为了获得一些反馈,我创建了frmWaiting形式,它显示了一个简单的不确定进度条。我还有一个表格的控制器, PrincipalController

申请的入口点

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:

@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:

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;
}

我想初始化我的主要表单frmPrincipal,当我的方法需要一段时间后才能完成。我也想让进度条工作。我试图在另一个线程上执行此操作,但是当方法完成时,我无法从中获得响应。

所有.fxml文件都是正确的,如果可能的话,可以省略它们。

就是这样,应用程序等待方法完成,然后打开另一个表单。但是,progressBar不会更新。

1 个答案:

答案 0 :(得分:1)

ArquivoController.getInstance().copiaArquivosPadrao();内,你必须更新progressBar的进度。

你可以这样做,使用Task来运行copiaArquivosPadrao(),相应地更新任务进度并绑定到任务进度属性。

或者你可以用丑陋的方式做到这一点,将progressBar传递给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.
}

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);
}

当然,你可以在循环或类似的环境中做得更精细。