I am trying to create a custom ListView from a custom Cell based on a list of custom objects .
A custom object is the name of a class called Message , which contains several fields for the message Content , recipient , timestamp, and status (read, send, etc.).
Having looked at this question: Configure ListView in JavaFX using FXML I successfully:
- created a ListView with custom cells , where the cell design is defined in the FXML file;
- tied controller so that each cell data item can be filled with the current collection item;
However, I was unable to bind the two: I cannot find a way for the current ListView to send the Cell Controller .
Here is my code for the factory cell and populating ListView elements:
final ObservableList observableList = FXCollections.observableArrayList(); observableList.setAll(myMessages); //assume myMessage is a ArrayList<Message> conversation.setItems(observableList); //the listview conversation.setCellFactory(new Callback<ListView<Message>, ListCell<Message>>() { @Override public ConversationCell<Message> call(ListView<Message> listView) { return new ConversationCell(); } });
And now the ConversationCell class:
public final class ConversationCell<Message> extends ListCell<Message> { @Override protected void updateItem(Message item, boolean empty) { super.updateItem(item, empty); ConversationCellController ccc = new ConversationCellController(null); setGraphic(ccc.getView()); } }
I cannot show the ConversationCellController, but all I can say is where (in its constructor) I load the FXML file that the cell designs, and then I can fill in the values ββwith this message.
The getView() method returns the root panel , which contains a cell with an already filled and constructed structure.
As I said, the project work, but I can not associate ListView elements with CellFactory , because in the method
protected void updateItem (message element, boolean empty)
empty set to true , and the item is really null .
What can I do to make this work?