我阅读了以下文档,http://docs.oracle.com/javafx/2/ui_controls/combo-box.htm而且我没有找到任何与我的需求相似的东西。我一直在寻找一种方法来将我的选项分组到一个组合框中。假设我的组合框是持续时间。我有以下选项:- 过去 1 小时、过去 2 小时、过去 24 小时、上周、过去 30 天、过去 3 个月、去年。我要在组合框中添加标签“持续时间短”和“持续时间长”。用户只能选择一个,但它看起来有点像:
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");
最佳答案
为您的组合框创建一个项目类,声明它是否是可选项目。 (您还可以向其添加其他有用的 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<ComboBoxItem> 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<ComboBoxItem>() {
@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);
}
}
更新 这个已经回复了elsewhere但我会使用 CSS PseudoClass 更改标题的样式和一个外部 CSS 文件:
添加
final PseudoClass header = PseudoClass.getPseudoClass("section-header");
到start(...)
方法,将cell factory的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 文件看起来像
combo-box-with-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 ;
}
关于java - 如何为组合框和列表中的选项添加标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26873045/