How to update tableview table using javaFx

I am trying to make some downloads and show progress inside my table:

enter image description here

To do this, I use the following classes:

public class DownloadDataTable { private SimpleDoubleProperty progress; private SimpleStringProperty type; private SimpleStringProperty status; public DownloadDataTable(double progress, String type, String status) { this.progress = new SimpleDoubleProperty(progress); this.type = new SimpleStringProperty(type); this.status = new SimpleStringProperty(status); } public double getProgress() { return progress.get(); } public void setProgress(double progress) { this.progress.set(progress); } public String getType() { String retorno; if (type==null){ retorno=""; }else{ retorno=type.get(); } return retorno; } public void setType (String type) { this.type.set(type); } public String getStatus(){ String retorno; if (status==null){ retorno=""; } else{ retorno=status.get(); } return retorno; } public void setStatus(String status){ this.status.set(status); } } 

and to create TitledPane tables, tableview and columns, I do this:

 public void addDownloadToTitledPane(DownloadContent downloadContent) { MetaDados metaDado = downloadContent.getMetaDado(); String title = metaDado.getName(); if (title.length() > 60) { title = title.substring(0, 57) + "..."; } TableView downloadTable = new TableView(); TableColumn<DownloadDataTable, Double> progress = new TableColumn<>("progress"); progress.setCellFactory(new Callback<TableColumn<DownloadDataTable, Double>, TableCell<DownloadDataTable, Double>>() { @Override public TableCell<DownloadDataTable, Double> call(TableColumn<DownloadDataTable, Double> p) { final ProgressBar progressBar = new ProgressBar(-1); final TableCell cell = new TableCell<DownloadDataTable, Double>() { @Override protected void updateItem(Double t, boolean bln) { super.updateItem(t, bln); if (bln) { setText(null); setGraphic(null); } else { progressBar.setProgress(t); progressBar.prefWidthProperty().bind(this.widthProperty()); setGraphic(progressBar); setContentDisplay(ContentDisplay.GRAPHIC_ONLY); } } }; cell.setAlignment(Pos.CENTER); return cell; } }); progress.setCellValueFactory(new PropertyValueFactory<DownloadDataTable, Double>("progress")); progress.setText("Progresso"); TableColumn<DownloadDataTable, String> type = new TableColumn<>("type"); type.setCellFactory(new Callback<TableColumn<DownloadDataTable, String>, TableCell<DownloadDataTable, String>>() { @Override public TableCell<DownloadDataTable, String> call(TableColumn<DownloadDataTable, String> p) { TableCell cell = new TableCell<DownloadDataTable, String>() { @Override public void updateItem(String item, boolean empty) { super.updateItem(item, empty); setText(empty ? null : getString()); setGraphic(null); } private String getString() { return getItem() == null ? "" : getItem().toString(); } }; cell.setAlignment(Pos.CENTER); return cell; } }); type.setCellValueFactory(new PropertyValueFactory<DownloadDataTable, String>("type")); type.setText("Tipo"); TableColumn<DownloadDataTable, String> status = new TableColumn<>("status"); status.setCellFactory(new Callback<TableColumn<DownloadDataTable, String>, TableCell<DownloadDataTable, String>>() { @Override public TableCell<DownloadDataTable, String> call(TableColumn<DownloadDataTable, String> p) { TableCell cell = new TableCell<DownloadDataTable, String>() { @Override public void updateItem(String item, boolean empty) { super.updateItem(item, empty); setText(empty ? null : getString()); setGraphic(null); } private String getString() { return getItem() == null ? "" : getItem().toString(); } }; cell.setAlignment(Pos.CENTER); return cell; } }); status.setCellValueFactory(new PropertyValueFactory<DownloadDataTable, String>("status")); status.setText("Status"); downloadTable.getColumns().addAll(progress, type, status); List<PendingComponents> pendingComponents = downloadContent.getPendingComponents(); ObservableList<DownloadDataTable> data = FXCollections.observableArrayList(); for (PendingComponents pendingComponent : pendingComponents) { String typeComponent; if (pendingComponent.getType().equalsIgnoreCase(Constants.HTML)) { typeComponent = "Conteúdo Principal"; } else { typeComponent = "Pacote de Imagens"; } data.add(new DownloadDataTable(-1, typeComponent, "Preparando download")); } downloadTable.setItems(data); downloadTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); TitledPane downloadPane = new TitledPane(title, downloadTable); downloadPane.setId(metaDado.getOfflineUuid()); vBoxDownload.getChildren().add(downloadPane); } 

So far, everything is working fine, but when I try to restore the table and update the data, my table is not updated. I am debbuged and everything seems to work, even the data value is changing, but my table is still without updating. See my code:

 private void publishProgress(final String msg) { Platform.runLater(new Runnable() { @Override public void run() { TitledPane titledPane = (TitledPane) controller.vBoxDownload.lookup("#"+metaDado.getOfflineUuid()); TableView table = (TableView) titledPane.getContent(); DownloadDataTable data = (DownloadDataTable) table.getItems().get(0); data.setProgress(100); data.setStatus(msg); } }); } 

If I try to delete and add my own line, this will not work, but if I just add another line with new values, I will have an old line with the same value and a new line with new values. I can’t understand what I’m doing wrong, can someone help me?

+4
source share
1 answer

You do not need to add / remove a row to update the table when the progress value changes.

The problem is that you are not making the progress property available to the TableView. This calls progress.setCellValueFactory (...) to wrap your getProgress () value in a new ObservableObjectWrapper. This allows the value to be displayed in the TableView, but it will not notify the table when the value changes.

Add the following to the DownloadDataTable class, and your table will update as the value changes.

 public SimpleDoubleProperty progressProperty() { return this.progress; } public SimpleStringProperty typeProperty() { return this.type; } public SimpleStringProperty statusProperty() { return this.status; } 
+8
source

Source: https://habr.com/ru/post/1494296/


All Articles