进展更新很晚

时间:2015-02-04 12:36:40

标签: java javafx javafx-2 javafx-8

您好我正在尝试让我的进度条在进程发生时进行更新。复制文件。我也输出到控制台,但我的进度条不会更新,直到它完成整个过程。

我做错了什么?这是我的代码,它是一个文件。

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

}

任何建议都是很棒的人

1 个答案:

答案 0 :(得分:1)

您正在FX线程中完成主要工作,因此您将阻止UI。

将复制代码放入Task中,让它在一个线程中运行。请查看TaskConcurrency文档。