Brian Bokoske Brian Bokoske - 2 months ago 11
Java Question

JavaFX ComboBox Displayed Item Font Size

I am using a combo box to display let the user pick between a few items in my JavaFX program but am having some trouble formatting it. I am able to change the font size of the items in the drop down list by modifying the cell factory but I am unable to figure out how to change the size of the singe displayed item when the combo box is sitting there not being used. I want to make the text a bit bigger the match the items in the list that I formatted in the cell factory. Below is a picture of what I am talking about. As you can see, the displayed item's font size is much smaller than the items in the drop down list. Any help is much appreciated.

CODE:

ComboBox countyList = new ComboBox(counties);
countyList.setPrefWidth(400);
countyList.setPrefHeight(35);
countyList.setCellFactory(l -> new ListCell<String>() {

@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);

if (empty || item == null) {
setText(null);
} else {
setText(item.toString());
setFont(HeldFont.build(15));
}
}

});


Picture of UI

Answer Source

try adding the following:

countyList.setButtonCell(new ListCell(){

        @Override
        protected void updateItem(Object item, boolean empty) {
            super.updateItem(item, empty); 
            if(empty || item==null){
                setStyle("-fx-font-size:15");
            } else {
                setStyle("-fx-font-size:15");
                setText(item.toString());
            }
        }

    });