我阅读了以下文档,即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);
}
}
更新这已在其他地方得到解答,但我将使用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文件可能看起来像
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 ;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句