Невозможно создать несколько индикаторов прогресса
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);
}
}