如何在combobox和列表中为选项添加标签?

我阅读了以下文档, http://docs.oracle.com/javafx/2/ui_controls/combo-box.htm ,但我找不到与我的需求相似的内容。 我正在寻找一种方法来将我的选项分组在一个combobox中。 假设我的combobox是持续时间。 我有以下选择: – 过去1小时,过去2小时,过去24小时,上周,过去30天,过去3个月,去年。 我要在combobox中添加标签“短持续时间”和“长持续时间”。 用户只能选择一个,但它会显示如下:

Short Duration Last Hour Last 2 hours Last 24 Hours Long Duration Last Month Last year 

持续时间短,持续时间长,就像标题一样。 你不能点击它们。

谢谢!

注意:我不是在谈论Label ab = new Label ("Short duration");

这是我的代码(我尝试在combobox中插入标签作为选项,但你可以选择它)

 ComboBox combobox_print_options = new ComboBox(); combobox_print_options.setPromptText("Choose the button you wish to click"); Label table = new Label("Table"); combobox_print_options.getItems().addAll( table, "a", "b"); 

为combobox创建一个项类,声明它是否是可选项。 (您还可以为此添加其他有用的API,例如它所代表的时间量的方便访问器。)

然后使用一个单元工厂来禁用表示不可选项的单元格:

 import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.ComboBox; import javafx.scene.control.ListCell; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class ComboBoxWithSections extends Application { @Override public void start(Stage primaryStage) { ComboBox combo = new ComboBox<>(); combo.getItems().addAll( new ComboBoxItem("Short Duration", false), new ComboBoxItem("Last Hour", true), new ComboBoxItem("Last 2 hours", true), new ComboBoxItem("Last 24 hours", true), new ComboBoxItem("", false), new ComboBoxItem("Long Duration", false), new ComboBoxItem("Last Month", true), new ComboBoxItem("Last Year", true) ); combo.setCellFactory(listView -> new ListCell() { @Override public void updateItem(ComboBoxItem item, boolean empty) { super.updateItem(item, empty); if (empty) { setText(null); setDisable(false); } else { setText(item.toString()); setDisable(! item.isSelectable()); } } }); BorderPane root = new BorderPane(null, combo, null, null, null); primaryStage.setScene(new Scene(root, 250, 400)); primaryStage.show(); } public static class ComboBoxItem { private final String name ; private final boolean selectable ; public ComboBoxItem(String name, boolean selectable) { this.name = name ; this.selectable = selectable ; } public String getName() { return name ; } public boolean isSelectable() { return selectable ; } @Override public String toString() { return name ; } } public static void main(String[] args) { launch(args); } } 

更新这已在其他地方得到解答,但我会使用CSS PseudoClass和外部CSS文件更改标题的样式 :

  final PseudoClass header = PseudoClass.getPseudoClass("section-header"); 

start(...)方法,并更改单元工厂的updateItem(...)方法,如下所示:

  public void updateItem(ComboBoxItem item, boolean empty) { super.updateItem(item, empty); if (empty) { setText(null); setDisable(false); pseudoClassStateChanged(header, false); } else { setText(item.toString()); setDisable(! item.isSelectable()); pseudoClassStateChanged(header, ! item.isSelectable()); } } 

现在将一个css文件附加到Scene

  scene.getStylesheets().add("combo-box-with-sections.css"); 

并且css文件可能看起来像

combobox与 – sections.css:

 .combo-box-popup .list-cell { -fx-padding: 4 0 4 20 ; } .combo-box-popup .list-cell:section-header { -fx-font: italic 10pt sans-serif ; -fx-padding: 4 0 4 5 ; }