2
  1. I load a listview with a list (a).
  2. In response to a toggle change, I switch the listview content to another list (b). If list (a) is longer than list (b), then the excess items remain displayed in the listview even though it is no longer selectable.

For instance, buyList = {'100532','100533'} and saleList = {'100000'}. If I start with radioBuy selected, the list will show '100532' and '100533'. Switching the toggle from radioBuy to radioSale will cause the list to display '100000' and '100533'. '100533' is not selectable, but it remains visible.

The observable lists themselves have been shown to be the correct size and contain the correct items. It's just that the ListView display shows some old data.

Things I've tried:
1. ticketListView.getItems().clear() and then rebuilding a new list before the list switch.
2. Using ticketListView.getItems().removeAll(0, ticketListView.getItems().size()) 3. Using a loop to remove each item individually. 4. Using ticketListView.refresh() in combination with all the above.

My toggle ChangeListener:

    typeGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
    public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
            if (typeGroup.getSelectedToggle().equals(radioBuy))
                ticketListView.setItems(buyList);
            if (typeGroup.getSelectedToggle().equals(radioTransfer))
                ticketListView.setItems(transList);
            if (typeGroup.getSelectedToggle().equals(radioSale))
                ticketListView.setItems(saleList);
        }
    });

How do I get my listview to blank itself before it switches content?

M. Teasdale
  • 323
  • 3
  • 17
  • Does your list view have a custom cell factory? If so, please include it in the question. – James_D Oct 25 '16 at 10:04
  • No custom cell factory. I just modified the toString method of the objects contained by the list, so the list can be considered a list of Strings. – M. Teasdale Oct 25 '16 at 14:41
  • OK, so my first guess is not correct. I've never seen this behavior: the expected behavior is that simply calling `setItems(...)` is enough to completely refresh the list view. I recommend you create a [MCVE] and [edit] your question to include it. – James_D Oct 25 '16 at 14:44
  • I tested the exact code you posted with the smallest application I could wrap around it that would make it compile and run, and it worked exactly as expected (the list was properly cleared each time). So something else somewhere in your code is causing the issue. – James_D Oct 25 '16 at 14:54
  • Sorry for wasting everyone's time. It turns out that I was indeed using a custom cell factory: – M. Teasdale Oct 25 '16 at 18:27
  • 1
    Presumably then this is a duplicate of http://stackoverflow.com/questions/26821319/javafx-listview-and-treeview-controls-are-not-repainted-correctly/26822360#26822360 – James_D Oct 25 '16 at 18:28
  • @M.Teasdale you didn't waste everyone's time, I happened to have the same issue (the code was initially mine, but I was not aware that another dev added the cell factory) – joH1 Jul 20 '21 at 09:54

2 Answers2

3

Sorry for wasting everyone's time. It turns out I was indeed using a custom cell factory:

ticketListView.setCellFactory(new Callback<ListView<Header>,
                                          ListCell<Header>>() {
                                      public ListCell<Header> call(ListView<Header> headerListView) {
                                          return new ListCell<Header>() {
                                              @Override
                                              protected void updateItem(Header item, boolean empty) {
                                                  super.updateItem(item, empty);
                                                  if (item != null) {
                                                      setText(item.getTicket());
                                                  }
                                              }
                                          };
                                      }
                                  }
    );

Removing this code and doing as I mentioned earlier, overriding toString, solved the problem. Obviously I need to study cell factories a little more...

M. Teasdale
  • 323
  • 3
  • 17
  • Or just fix the cell factory according to the [documentation](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Cell.html#updateItem-T-boolean-) – James_D Oct 25 '16 at 19:20
1
if (item != null) {
    setText(item.getTicket());
} else {
    setText(null);
}

You should add this code to clear empty cells.

Chun
  • 31
  • 2