2017-07-17 88 views
0

我正在使用组合框来显示让用户在我的JavaFX程序中的几个项目之间进行选择,但我在格式化时遇到了一些问题。我可以通过修改单元格工厂来更改下拉列表中的项目的字体大小,但当组合框坐在那里时,我无法知道如何更改单独显示项目的大小。我想让文本稍微大一点,以便在单元格工厂中格式化的列表中的项目匹配。以下是我正在谈论的内容。正如您所看到的,显示的项目的字体大小比下拉列表中的项目要小得多。任何帮助深表感谢。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

回答

0

尝试添加以下内容:

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

    }); 
+0

完美!像魅力一样工作。非常感谢! –

+0

很高兴帮助)) –