Brett
Brett

Reputation: 386

Progress updateing late

Hi I'm trying to get my progress bar to update as a process is happening. Copying a file. I am also outputting to the console but my progress bar won't update until it has finished the process.

What am I doing wrong? here is my code, it is one file.

package tvconvertversion3;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.control.ProgressBar;

/**
 *
 * @author brett
 */
public class CreateWorkFile extends Application {

    private final File fileIn = new File("C:\\Users\\brett\\Documents\\Humans Need Not Apply-7Pq-S557XQU.mp4");
    private Task task;
    private File fileOut;
    private ProgressBar mpgb;

    @Override
    public void start(Stage primaryStage) {
        mpgb = new ProgressBar();
        mpgb.setVisible(true);

        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
                Thread th = new Thread(task);
                th.setDaemon(true);
                th.start();
            }
        });
        task = new Task<Integer>() {
            @Override
            protected Integer call() throws Exception {
                fileOut = new File("C:\\Users\\brett\\Documents\\ConvertedTvFiles\\Tenacious D - 39 HQ-86LT83IFDfQ.mp4");
                try {
                    FileInputStream fin;
                    long length = fileIn.length();
                    long counter = 0;
                    int r;
                    byte[] b = new byte[1024];
                    fin = new FileInputStream(fileIn);
                    FileOutputStream fout = new FileOutputStream(fileOut);
                    while ((r = fin.read(b)) != -1) {
                        mpgb.setProgress(100 * counter / length);
                        counter += r;
                        System.out.println(1.0 * counter / length);
                        fout.write(b, 0, r);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(CreateWorkFile.class.getName()).log(Level.SEVERE, null, ex);
                }
                return null;

            }
        };

        StackPane root = new StackPane();
        StackPane.setAlignment(mpgb, Pos.BOTTOM_CENTER);
        root.getChildren().addAll(btn, mpgb);
        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

Any advice would be awesome guys

Upvotes: 0

Views: 51

Answers (1)

Roland
Roland

Reputation: 18415

You are doing the main work in the FX thread and hence you're blocking the UI.

Put the copying code into a Task and let it run in a thread. Take a look at the Task and Concurrency documentation.

Upvotes: 1

Related Questions