What I understand from the documentation of JavaFX is that, when there are insufficient number of cells to fill the provided area of the ListView (or else like), new cells are generated. Otherwise, existing cells are reused, by calling updateItem method on each with the corresponding items.
I am implementing a chat application, in which the user writes something to the textarea, presses enter and the chat is appended to the history above the text area, very simple&classic. The history is a ListView, showing list of chats sent/received so far. So when the user presses enter, I add a new item to the underlying observable list, so it shows up in the ListView.
The problem is, when a new chat is appended to the ListView, there is some kind of a blink in the list, it takes a little time to update the items in the list. However, the newly added chat is at the end of the list, and it is not visible to the user (needs to scroll down, for now). So actually it is not needed to update the already visible cells, but it does. I tried to simplify the content of the updateItem method, but no way... It is always blinking.
Then I implemented a simple class like;
public class IdGenerator {
private static IdGenerator instance = new IdGenerator();
private int id = 0;
public static IdGenerator getInstance() {
return instance;
}
public synchronized int getId() {
return ++id;
}
}
and then assigned ids to the generated cells by;
public class CommunicationEntityCell extends ListCell<CommunicationEntity> {
...
private int id = IdGenerator.getInstance().getId();
....
@Override
protected synchronized void updateItem(CommunicationEntity entity, boolean empty) {
super.updateItem(entity, empty);
System.out.println(id);
....
}
}
And I observed that, when a new item is added to the underlying list, the cells are reproduced, not recycled. (And probably this is reason of that blink, anyway)
Is it normal/logical/expected for cells to be reproduced? Is this a (known) bug? Have you ever seen this? Any workaround would be highly appreciated.