我不知道如何在下载时更新每个指定行的进度条以查看下载文件的进度
这是Controller类的一段代码
type here @FXML
public void btnInsert () throws IOException {
String urlText = this.downloadUrl.getText().trim(); // Trim to remove leading/trailing spaces
URL url = null;
try {
url = new URL(urlText);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
String fileName = Paths.get(url.getFile()).getFileName().toString();
long fileSize = 0;
try {
fileSize = url.openConnection().getContentLengthLong();
} catch (IOException e) {
throw new RuntimeException(e);
}
String path = null;
Url obj = new Url(fileName,fileSize,"waiting",0.1,urlText, path);
table.getItems().add(obj);
// Update the progress bar for the newly inserted row
int newIndex = table.getItems().size() - 1;
// Update the progress for the specified row
Url rowItem = table.getItems().get(newIndex);
rowItem.setProgress(0.1);
// Refresh the cell to update the progress bar for the given row
table.refresh();
}
private void downloadFileAll(String urlText,String downloadPath,int place) throws MalformedURLException {
if (urlText.isEmpty()) {
// Alert user if URL is empty
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("URL not found!");
alert.setHeaderText("Please provide the URL!");
alert.show();
}
URL url = new URL(urlText);
String fileName = Paths.get(url.getFile()).getFileName().toString();
String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
System.out.println("File extension: " + extension);
// Assuming table is your TableView instance
int rowIndex = 0; // Index of the row containing the ProgressBar
int columnIndex = 3; // Index of the column containing the ProgressBar
TableColumn<Url, Double> progressColumn = (TableColumn<Url, Double>) table.getColumns().get(columnIndex); // Replace 'columnIndex' with the actual index of the TableColumn
DownloadFile downloadFile = new DownloadFile(urlText, downloadPath +"\\"+ fileName +"."+extension, this.progressLabel);
executor.submit(() -> {
try {
downloadFile.run();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
public void run() {
try {
URL url = new URL(this.url);
InputStream in = url.openStream();
FileOutputStream out = new FileOutputStream(this.downloadPath);
byte[] buffer = new byte[1024];
long fileSize = url.openConnection().getContentLengthLong();
long totalBytesRead = 0L;
int bytesRead;
while((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
totalBytesRead += (long)bytesRead;
long finalTotalBytesRead = totalBytesRead;
Platform.runLater(() -> {
double progressValue = (double) finalTotalBytesRead / (double)fileSize;
this.progress.set(progressValue);
String progressText = String.format("%.0f%%", progressValue * 100.0);
this.progessLabel.setText(progressText);
});
}
in.close();
out.close();
System.out.println("Successfully download");
Status = "Done";
} catch (Exception var12) {
var12.printStackTrace();
System.out.println("Download Failed");
}
}
我尝试获取控制器类的表并在 DownloadFile 类中更新它,但它对我不起作用,我尝试了这个:
// public TableView<Url> table;
// public void accessTableView(TableView<Url> table) {
// FXMLLoader loader = new FXMLLoader(getClass().getResource("hello-view.fxml"));
// try {
// Parent root = loader.load();
// HelloController helloController = loader.getController();
//
// table = helloController.gettable();
//
// // Now you can work with the TableView
// // For example, add items to it
//// table.getItems().addAll(new Url(...));
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
如果我没理解错的话,你想显示一个像steam上的下载栏一样的栏吗? 如何在javafx中下载时显示进度条 和 https://www.w3resource.com/java-exercises/javafx/javafx-user-interface-components-exercise-18.php
在我看来他们可以提供帮助