如何保持“空"在JavaFx的组合框列表中选择? [英] How to keep "empty" selection on my combobox list in JavaFx?

查看:65
本文介绍了如何保持“空"在JavaFx的组合框列表中选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个组合框,其中包含来自数据库的一些值(一些实时更新的列表).此组合框列表每1分钟更新一次.列表没有空值.当我将此列表设置为ComboBox时.

In my application I hava combobox which is holding some values from databse ( some real time updated list ). This ComboBox list is updated every 1 minute. List dosen't have null values. When I'm setting this list to ComboBox..

ComboBox box = new ComboBox(items);

..还有一个额外的空"行,这很好,因为选择了非值.在选择一些值后,我的空"值从列表中消失了.我的主要问题是如何在列表中保留此值?

.. there is one extra "empty" row, which is perfectly fine because non value is selected. Right after I'm selecting some value my "empty" value disappears from the list. My main question is How to keep this value on the list?

为什么这是一个问题.

  1. 在数据库中选择了场景值,首先启动了应用程序
  1. Scenerio values is selected in database, first application start
  1. 列表已加载(具有选定的空值).
  2. 已选择值.
  3. 在第一次后台刷新期间,空值消失,并且组合框值被选择为n + 1,然后选择了下一个值.

  • 如果要选择空值,则必须通过选择模型/通过一些额外的控制将所有clearSelection清除.
  • 推荐答案

    虽然这是一个老问题,但我花了很多时间尝试解决应用程序中的同一问题,并认为我也可以在这里添加解决方案.

    While it is an old question, I spent quite bit of time trying to solve the same issue in my application and thought i might as well add my solution here.

    一个可能的解决方法是创建一个额外的列表,其中包含 null 和您希望选择的项目.

    One possible workaround is to create an extra list that contains null and the items you wish to be selectable.

    ObservableList<String> selectableActivities = FXCollections.observableArrayList("a", "b", "c");
    ObservableList<String> selectableActivitiesWithNull = FXCollections.observableArrayList();;
    selectableActivitiesWithNull.add(null);
    selectableActivitiesWithNull.addAll(selectableActivities);
    

    为了支持原始列表的更新,您需要一个 ListChangeListener ,它可以根据原始列表中的更改来更新额外的列表.

    In order to support updates of the original list you would need a ListChangeListener that updates the extra list according to changes in the original list.

    selectableActivities.addListener((ListChangeListener<String>)(change -> {
        while (change.next()) {
            if (change.wasPermutated()) {
                selectableActivitiesWithNull.sort((a, b) -> {
                    return Integer.compare(selectableActivities.indexOf(a), selectableActivities.indexOf(b));
                });
            } else if (change.wasRemoved()) {
                selectableActivitiesWithNull.remove(change.getFrom()+1, change.getTo()+2);
            } else if (change.wasAdded()) {
                selectableActivitiesWithNull.addAll(change.getFrom()+1, selectableActivities.subList(change.getFrom(), change.getTo()));
            } else if (change.wasUpdated()) {
                for (int i = change.getFrom(); i < change.getTo(); ++i) {
                    selectableActivitiesWithNull.set(i+1, selectableActivities.get(i));
                }
            }
        }
    }));
    

    最后,您将额外的列表用于ComboBox项.

    And finally you use the extra list for the ComboBox items.

    ComboBox<String> combobox = new ComboBox<String>();
    combobox.setItems(selectableActivitiesWithNull);
    

    现在,您可以像往常一样修改原始列表,并且ComboBox会相应更新,同时第一项的选择也为空.最重要的是,您的原始列表不会被占位符对象污染,这些占位符对象可能会导致应用程序其他部分出现问题.

    Now you can modify the original list as usual and the ComboBox will update accordingly while also having an empty selection as the first item. And most importantly your original list will not be polluted by placeholder objects that could cause issues in other parts of the application.

    如果您将适当的 StringConverter 添加到ComboBox,这也将与其他对象一起使用.请注意,如果使用上述方法,转换器还必须能够处理空值.

    This will also work with other objects, assuming that you add an apropriate StringConverter to the ComboBox. Note that the converter must also be able to handle null values if using the above approach.

    StringConverter<Object> activityConverter = new StringConverter<Object>() {
        @Override
        public String toString(Object object) {
            return object != null ? object.toString() : "";
        }
        @Override
        public ActivityDataRow fromString(String string) {
            return null;
        }
    };
    
    combobox.setConverter(activityConverter);
    

    虽然这种方法并不完全符合您的期望,但我相信这是无需实施自定义组合框即可获得的结果.

    While this approach is not exactly what you desired, I believe this is a close you can get without implementing a custom combobox.

    这篇关于如何保持“空"在JavaFx的组合框列表中选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆