I am trying to write a simple color chooser ComboBox in JavaFX 15. I want the dropdown menu to contain a large number of very small cells. Unfortunately, when I set the cell size and increase the number of visible rows, many of them appear empty. It appears that the program is computing the number of cells to render based on how many fit into the ListView prior to resizing.
Changing the number of visible rows or the size of the ListView results in different numbers of cells being rendered, but the fraction of empty cells remains the same. I've tried to using the hide() and show() methods to force the ComboBox to draw itself again (as suggested here: Javafx combobox not updating dropdown size upon change on realtime?) and I've tried replacing the backgrounds with Rectangles and fiddling with the CSS and Property binding, but so far nothing has worked.
Here's my code: Any suggestions?
public class ColorComboBox extends ComboBox {
String[] colorCodes = {"#CD5C5C", "#F08080", "#FA8072", "#E9967A", "#FFA07A", "#DC143C", ...
ColorComboBox() {
setCellFactory(lv -> new ColorCell());
setVisibleRowCount(30);
getItems().addAll(colorCodes);
}
public class ColorCell extends ListCell<String> {
@Override
public void updateItem(String color, boolean empty) {
super.updateItem(color, empty);
setPrefSize(40, 6);
if(empty)
setBackground(null);
else
setBackground(new Background(new BackgroundFill(Color.web(color), CornerRadii.EMPTY, Insets.EMPTY)));
}
}
}
