Как создать файл с несколькими файлами прогресса Java. - PullRequest
0 голосов
/ 10 октября 2019

Невозможно создать несколько индикаторов прогресса

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;

    import javafx.application.Application;
    import javafx.concurrent.Task;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.control.ProgressBar;
    import javafx.scene.control.ProgressIndicator;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;

    public class FileDownload extends Application {

        public void start(Stage primaryStage) throws Exception {
            primaryStage.setScene(new Scene(createContent()));
            primaryStage.show();
        }

        private Parent createContent() {
            VBox root = new VBox();
            root.setPrefSize(400, 600);
            TextField fieldUrl = new TextField();
            root.getChildren().addAll(fieldUrl);

            fieldUrl.setOnAction(event -> {
                for (final File fileEntry : new File(fieldUrl.getText()).listFiles()) {
                    Task<File> task = new Download(fileEntry);
                    ProgressBar pb = new ProgressBar();
                    ProgressIndicator progressIndicator = new ProgressIndicator();
                    Label statusLabel = new Label();
                    pb.setPrefWidth(350);
                    pb.progressProperty().bind(task.progressProperty());
                    progressIndicator.progressProperty().unbind();
                    progressIndicator.progressProperty().bind(task.progressProperty());
                    statusLabel.textProperty().unbind();
                    statusLabel.setText(fileEntry.getName());
                    root.getChildren().add(statusLabel);
                    root.getChildren().add(pb);
                    root.getChildren().add(progressIndicator);
                    Thread thread = new Thread(task);
                    thread.setDaemon(true);
                    thread.start();

                }
            });

            return root;
        }

        private class Download extends Task {

            private File file;

            public Download(File fileEntry) {
                this.file = fileEntry;
            }

            @Override
            protected File call() throws Exception {
                try {
                    FileInputStream fis = new FileInputStream(file);
                    FileOutputStream fos = new FileOutputStream("/Users/manish/Desktop/testsuccess/" + file.getName());

                    long nread = 0L;
                    byte[] buf = new byte[8192];
                    int n;
                    while ((n = fis.read(buf)) > 0) {
                        fos.write(buf, 0, n);
                        nread += n;
                        updateProgress(nread, file.length());
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return file;

            }

            protected void failed() {
                System.out.println("failed");
            }

            protected void succeeded() {
                System.out.println("succeeded");
            }

        }

        public static void main(String args[]) {
            launch(args);
        }

    }

1 Ответ

0 голосов
/ 10 октября 2019

Я реализовал ProgressMonitor , который позволяет отслеживать ход выполнения списка работников. Основная цель - разместить этот монитор в строке состояния. Может быть, вы найдете это полезным или вы можете взглянуть на исходный код для получения дополнительных советов.

Библиотека доступна в Maven Central:

<dependency>
  <groupId>org.drombler.commons</groupId>
  <artifactId>drombler-commons-fx-core</artifactId>
  <version>0.13</version>
</dependency>

Исходный код включен GitHub .

...