0

I have a ListView in JavaFX and when I'm deleting a item there will show up more items in the ListView but not in the list.

Declaration of ObservableList and Extractor

Callback<ElectricDeviceType, Observable[]> deviceTypeExtractor = new Callback<ElectricDeviceType, Observable[]>() {
    @Override
    public Observable[] call(ElectricDeviceType t) {
        return new Observable[] { t.maxValue, t.name, t.getDevices() };
    }
};
ObservableList<ElectricDeviceType> deviceTypes = FXCollections
        .observableArrayList(deviceTypeExtractor);    

Here I'm setting the Devices to the ListView

    public void setElectricDevices(ElectricDeviceType... list) {
    deviceTypes.addAll(list);
    deviceTypeList.setItems(deviceTypes);
    deviceTypeList
            .setCellFactory(new Callback<ListView<ElectricDeviceType>, javafx.scene.control.ListCell<ElectricDeviceType>>() {
                @Override
                public ListCell<ElectricDeviceType> call(
                        ListView<ElectricDeviceType> listView) {
                    return new ElectricDeviceTypeListViewCell(_this,
                            simulation);
                }
            });
}

Class ElectricDeviceTypeListViewCell

public class ElectricDeviceTypeListViewCell extends ListCell<ElectricDeviceType> {
private MainWindowController controller;
private Simulation simulation;

public ElectricDeviceTypeListViewCell(MainWindowController c, Simulation s) {
    controller = c;
    simulation = s;
}

@Override
public void updateItem(ElectricDeviceType t, boolean empty) {
    super.updateItem(t, empty);
    if (t != null) {
        ElectricDeviceTypeController data = new ElectricDeviceTypeController(controller, simulation);
        data.setInfo(t);
        setGraphic(data.getBox());
    }
}

}

When I delete one item from my observable list, my listview shows 5 items instead of 2. But my observable only has 2 items inside. If I delete all of my items then i the duplicates are deleted too.

Before delete

After delete

Could you please help me with that?

Drast
  • 45
  • 2
  • 9
  • You don't know how the cell mechanism is implemented (and even if you do, you shouldn't rely on the implementation staying the same). There's no guarantee the list view will reuse the same cell that was displaying the removed item for the new one you add. Please try the suggested solution. – James_D Dec 15 '14 at 16:29
  • Thank you, I tried it and it worked :) – Drast Dec 15 '14 at 16:31

1 Answers1

4

I guess you should update your ElectricDeviceTypeListViewCell to change its cell upon update for empty and null values, too:

@Override
public void updateItem(ElectricDeviceType t, boolean empty) {
    super.updateItem(t, empty);
    if(empty || t == null) {
      setGraphic(null);
      setText(null);
    }
    else {
        ElectricDeviceTypeController data = new ElectricDeviceTypeController(controller, simulation);
        data.setInfo(t);
        setGraphic(data.getBox());
    }
}
eckig
  • 10,964
  • 4
  • 38
  • 52
  • This is already answered several times on SO, ([here](http://stackoverflow.com/questions/26821319/javafx-listview-and-treeview-controls-are-not-repainted-correctly) for example). – James_D Dec 15 '14 at 16:29
  • thank you! that was the solution – Drast Dec 15 '14 at 16:30
  • @James_D I was already typing when you submitted your comment. StackOverflow Multithreading went wrong ;-) But your are right, this might be flagged as duplicate. – eckig Dec 15 '14 at 16:31
  • @eckig No worries (SO search doesn't find solutions as easily as it might.) And yes, these provide perfect examples for discussions on various session context strategies for web apps :). – James_D Dec 15 '14 at 16:33