JavaFX custom cell factory with custom Objects

后端 未结 1 2025
失恋的感觉
失恋的感觉 2020-12-01 13:26

I am trying to have a custom ListView made of custom Cell based on a list of custom objects.

The custom object is class name c

相关标签:
1条回答
  • 2020-12-01 13:49

    All custom cell implementations that override updateItem(...) need to deal with the case where the cell is empty in that method. So you could do a naïve fix of this with

    public final class ConversationCell<Message> extends ListCell<Message> { 
    
        @Override
        protected void updateItem(Message item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setGraphic(null);
            } else {
                // did you mean to pass null here, or item??
                ConversationCellController ccc = new ConversationCellController(null);
                setGraphic(ccc.getView());
            }
        }
    }
    

    However, this is not a good solution from the point of view of performance. You are loading the FXML every time updateItem(...) is called with a non-empty cell, and that's a pretty expensive operation (potentially involving file i/o, unzipping the FXML file from a jar file, parsing the file, lots of reflection, creating new UI elements, etc). You don't want to be asking the FX Application Thread to be doing all that work every time the user scrolls the list view by a few pixels. Instead, your cell should cache the node and should update it in the updateItem method:

    public final class ConversationCell<Message> extends ListCell<Message> { 
    
        private final ConversationCellController ccc = new ConversationCellController(null);
        private final Node view = ccc.getView();
    
        @Override
        protected void updateItem(Message item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setGraphic(null);
            } else {
                ccc.setItem(item);
                setGraphic(view);
            }
        }
    }
    

    You should define a setItem(...) method in the ConversationCellController that updates the view (sets text on labels, etc etc) accordingly.

    0 讨论(0)
提交回复
热议问题