简体   繁体   中英

Java FX - Progress bar update

I have a method that takes a while to complete, when the jar application is started. To have some feedback, i created the form frmWaiting , that displays a simple indeterminate progress bar. I also have a controller for the form, 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:

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

I want to initialize my main form, frmPrincipal , when my method that takes a while finishes. 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.

The way it is, the application waits for the method to finish, then opens the other form. But, the progressBar does not update.

Inside ArquivoController.getInstance().copiaArquivosPadrao(); you have to update the progressBar 's progress.

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.

Or you could do it the ugly way, passing progressBar to copiaArquivosPadrao() , something like this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM