ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 如何在组合框和列表中为选项添加标签?

2019-08-30 09:59:41  阅读:261  来源: 互联网

标签:java javafx combobox


我阅读了以下文档,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,javafx,combobox
来源: https://codeday.me/bug/20190830/1767463.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有