Let’s use a cell factory to display nodes in the button area and the pop-up area of a combo box. Listing 12-14 will be our starting point. It declares a StringShapeCell class, which inherits from the ListCell<String> class. You need to update its content in its updateItem() method, which is automatically called. The method receives the item, which in this case is String, and a boolean argument indicating whether the cell is empty. Inside the method, you call the method in the superclass first. You derive a shape from the string argument and set the text and graphic in the cell. The shape is set as the graphic. The getShape() method returns a Shape from a String.

Listing 12-14. A Custom ListCell that Displays a Shape and Its Name

// StringShapeCell.java ...

Get Learn JavaFX 8: Building User Experience and Interfaces with Java 8 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.