JavaFX - Custom Cell Rendering ListView, Table, etc. ---------------------------------------------------------------------------- -- API SAMPLE ---------------------------------------------------------------------------- lvNotes is the ListView we're working with. We want a ListView item containing two stacked labels with the top one (title) bold. ASSIGN A NEW CELLFACTORY TO THE DESIRED LISTVIEW... * NoteCellData is a class containing data assigned to each item/cell This class is what's added via lvNotes.getItems().add() * NoteListCell is a cell object/class defined below lvNotes.setCellFactory(new Callback, ListCell>() { @Override public ListCell call(ListView list) { return new NoteListCell(); } }); REQUIRED CLASSES... /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * CLASS NoteCellData * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Class used to house data for one list item/cell *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ public class NoteCellData { private String _title; public String getTitle() { return this._title; } private String _details; public String getDetails() { return this._details; } public NoteCellData(String title, String details) { this._title = title; this.details = details; } } /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * CLASS NoteListCell * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * It's called whe a cell is needed. It creates an instance of NoteCellContents and * calls it's setInfo() method passing the NoteCellData object for that cell's instance. * Then it calls the setGraphic() method passing the outer object from the NoteCellContents object *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ public class NoteListCell extends ListCell { public NoteListCell() { } @Override protected void updateItem(NoteCellData item, boolean empty) { super.updateItem(item, empty); if(item != null) { NoteCellContents ncc = new NoteCellContents(); ncc.setInfo(item); setGraphic(ncc.getBox()); } } } /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * CLASS NoteCellContents * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * setInfo() populates the elements with the appropriate data * getBox() returns the outer container (in this case hBoxMain) * Note: This can also be accomplished via FXML, but not deisred for this example *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ public class NoteCellContents { private HBox hBoxMain = new HBox(); private VBox vBox = new VBox(); private Label lblTitle = new Label(); private Label lblDetail = new Label(); public NoteCellContents() { Font f = lblTitle.getFont(); f = Font.font(f.getFamily(), FontWeight.BOLD, f.getSize()); lblTitle.setFont(f); vBox.getChildren().add(lblTitle); vBox.getChildren().add(lblDetail); hBoxMain.getChildren().add(vBox); } public void setInfo(NoteCellData ncd){ lblTitle.setText(ncd.getTitle()); lblDetail.setText(ncd.getDetails()); } public HBox getBox() { return hBoxMain; } } ---------------------------------------------------------------------------- -- FXML SAMPLE -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ See: http://stackoverflow.com/questions/19588029/customize-listview-in-javafx-with-fxml