I'm learning JavaFX and i wanted to create a cell factory which is working properly until i want to delete a row from my ListView
:
plateList.setCellFactory(new Callback<ListView<Car>, ListCell<Car>>() {
@Override
public ListCell<Car> call(ListView<Car> param) {
ListCell<Car> cell = new ListCell<Car>() {
@Override
protected void updateItem(Car item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item.getPlate());
}
}
};
return cell;
}
});
I'm populating the ListView
with some sample data:
ObservableList<Car> sample = FXCollections.observableArrayList();
sample.add(new Car("123-abc", "opel", "corsa", 5.5));
sample.add(new Car("123-cba", "vw", "passat", 7.5));
plateList.setItems(sample);
Now i will see what i expect the ListView
will be the following:
How ever if delete a row ex: the first row (123-abc), the ListView
will look like this:
This is the delete part:
@FXML
private void deleteBtnAction() {
plateList.getItems().remove(plateList.getSelectionModel().getSelectedItem());
ObservableList<Car> t = plateList.getItems();
plateList.setItems(t);
}
If i remove the cell factory the program works as intended.
Any help is greatly appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…