Properly override updateItem method of list cells

This commit is contained in:
Kai S. K. Engelbart 2020-06-10 11:33:49 +02:00
parent 8a3ffec859
commit d14cc47365
2 changed files with 12 additions and 4 deletions

View File

@ -28,7 +28,10 @@ public class ContactListCell extends ListCell<Contact> {
@Override
protected void updateItem(Contact contact, boolean empty) {
super.updateItem(contact, empty);
if (!empty && contact != null) {
if (empty || contact == null) {
setText(null);
setGraphic(null);
} else {
// the infoLabel displays specific contact info, i.e. status of a user or amount
// of members in a group
Label infoLabel = null;
@ -51,7 +54,7 @@ public class ContactListCell extends ListCell<Contact> {
break;
}
infoLabel.setTextFill(textColor);
} else
} else
// group specific infos
infoLabel = new Label(String.valueOf(((Group) contact).getContacts().size()) + " members");
setGraphic(new VBox(new Label(contact.getName()), infoLabel));

View File

@ -45,10 +45,15 @@ public class MessageListCell extends ListCell<Message> {
@Override
protected void updateItem(Message message, boolean empty) {
super.updateItem(message, empty);
setGraphic(!empty && message != null ? new HBox(
if(empty || message == null) {
setText(null);
setGraphic(null);
} else {
setGraphic(new HBox(
new VBox(
new Label(dateFormat.format(message.getCreationDate())),
new Label(message.getText())),
new Label("", new ImageView(statusImages.get(message.getStatus())))) : null);
new Label("", new ImageView(statusImages.get(message.getStatus())))));
}
}
}