java – 如何在组合框和列表中为选项添加标签?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 如何在组合框和列表中为选项添加标签?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3302字,纯文字阅读大概需要5分钟。
内容图文
![java – 如何在组合框和列表中为选项添加标签?](/upload/InfoBanner/zyjiaocheng/725/4d3a21f5fb4a497c8c226768ec4d0358.jpg)
我阅读了以下文档,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
持续时间短,持续时间长,就像标题一样.你不能点击它们.
谢谢!
注意:我不是在谈论标签ab =新标签(“短期”);
这是我的代码(我尝试在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(…)方法,并更改单元工厂的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.getStylesheets().add("combo-box-with-sections.css");
和css文件看起来像
组合框与 – 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 – 如何在组合框和列表中为选项添加标签?全部内容,希望文章能够帮你解决java – 如何在组合框和列表中为选项添加标签?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。