JavaFX ComboBox显示的项目字体大小

时间:2017-07-17 12:53:47

标签: java user-interface javafx combobox formatting

我正在使用一个组合框来显示让用户在我的JavaFX程序中的几个项目之间进行选择,但是在格式化它时遇到了一些麻烦。我可以通过修改单元格工厂来更改下拉列表中项目的字体大小,但是当组合框未被使用时,我无法弄清楚如何更改单个显示项目的大小。我想让文本比我在单元格工厂中格式化的列表中的项目更大一些。下面是我正在谈论的图片。如您所见,显示项目的字体大小远小于下拉列表中的项目。非常感谢任何帮助。

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

1 个答案:

答案 0 :(得分:1)

尝试添加以下内容:

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());
            }
        }

    });
相关问题