Archivio | Coding RSS for this section

JavaFX – Avoid ghost duplicated elements in ListView

Recently I’ve started learning GUI development with JavaFX. To do so I decided to code a Contacts app capable of adding, removing, editing and showing a list of Person. My idea was having a side bar with a ListView and the rest for showing selected contact details.

I wanted to customize the listView in order to show last name with a bold font,  first name regular and a profile picture on the right of listview cell. JavaDoc was helpful for this and according to it, extending ListCell<E> and overriding the updateItem() method was enough. So I wrote this:

public class ContactsListCell extends ListCell<Person> {
    @Override
    protected void updateItem(Person item, boolean empty) {
        super.updateItem(item, empty);

        if (!empty) {
            try {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("../View/CellLayout.fxml"));
                GridPane cellLayout = (GridPane) loader.load();
                CellLayoutController cellLayoutController = loader.getController();
                cellLayoutController.CellSetup(item);
                setGraphic(cellLayout);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

Where CellLayoutController class is: Continua a leggere…

Annunci