I am using an ObservableList
to control the items and every time I delete an item, the TableView
removes it from the datasource. But the view is not being updated as I'm still seeing all the items. The only difference is that the removed item can not be selected anymore.
Similar problem: javafx listview and treeview controls are not repainted correctly
In the following the code:
final TableColumn<TMachineType, String> testerType = new TableColumn<TMachineType, String>(
bundle.getString("table.testerType"));
testerType
.setCellValueFactory(new PropertyValueFactory<TMachineType, String>(
"testerType"));
testerType
.setCellFactory(new Callback<TableColumn<TMachineType, String>, TableCell<TMachineType, String>>() {
@Override
public TableCell<TMachineType, String> call(
final TableColumn<TMachineType, String> param) {
final TableCell<TMachineType, String> cell = new TableCell<TMachineType, String>() {
@Override
protected void updateItem(final String item,
final boolean empty) {
super.updateItem(item, empty);
textProperty().unbind();
if (empty || item == null) {
setGraphic(null);
setText(null);
}
if (!empty) {
final TMachineType row = (TMachineType) getTableRow().getItem();
textProperty().bind(
row.testerTypeProperty());
}
}
};
return cell;
}
});
TMachineType class:
private final SimpleStringProperty testerType = new SimpleStringProperty();
@Column(name = "TESTER_TYPE")
public String getTesterType() {
return testerType.get();
}
public void setTesterType(String testerType) {
this.testerType.set(testerType);
}
public StringProperty testerTypeProperty() {
return testerType;
}
Observable List: 1. DB entities:
final Query q = em.createQuery("SELECT t FROM TMachineType t");
final List resultList = q.getResultList();
2. Obs. list setup:
ObservableList<TMachineType> observableList;
...
observableList = FXCollections.observableArrayList(resultList);
tableMachineType.setItems(observableList);
FXCollections.sort(observableList);
Row removal:
@FXML private void handleOnRemove(final ActionEvent event) { final ObservableList<TMachineType> selectedIndices = tableMachineType .getSelectionModel().getSelectedItems(); final String infoTxt = selectedIndices.size() + " " + bundle.getString("message.records_removed"); final List<TMachineType> deleteBuffer = new ArrayList<TMachineType>(); deleteBuffer.addAll(selectedIndices); for (final TMachineType selectedIdx : deleteBuffer) { selectedIdx.manufacturerProperty().unbind(); selectedIdx.testerTypeProperty().unbind(); deleted.add(selectedIdx); observableList.remove(selectedIdx); // tableMachineType.getItems().remove(selectedIdx); } ..
}