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

查看:108
本文介绍了如何保持“空”状态?在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天全站免登陆