I am learning JavaFX and I need to create a factory cell that works correctly until I want to remove 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 populate the ListView few data examples:
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 that I expect the ListView be as follows:
However, if you delete the line ex: the first line (123-abc), the ListView will look like this:
This is part of the removal:
@FXML private void deleteBtnAction() { plateList.getItems().remove(plateList.getSelectionModel().getSelectedItem()); ObservableList<Car> t = plateList.getItems(); plateList.setItems(t); }
If I delete the factory cell, the program works as intended. Any help is appreciated.
source share