【问题标题】:how to create java fx multiple progress bar file download如何创建java fx多个进度条文件下载
【发布时间】:2019-10-10 03:20:57
【问题描述】:

无法创建多个进度条

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

    }

【问题讨论】:

标签: multithreading javafx task


【解决方案1】:

我已经实现了一个ProgressMonitor,它允许监控工作人员列表的进度。主要目的是将这个监视器放在状态栏中。也许您觉得它很有用,或者您可以查看源代码以获取更多提示。

该库可从 Maven Central 获得:

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

源代码在GitHub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多