简体   繁体   English

将HashMap绑定到ComboBox

[英]Binding HashMap to ComboBox

Does anybody have an example of how to binding the keys of a HashMap to a ComboBox so that the changes in the HashMap are instantly reflected to the ComboBox? 有没有人举例说明如何将HashMap的键绑定到ComboBox,以便HashMap中的更改立即反映到ComboBox?

Thanks! 谢谢!

EDIT: 编辑:

Solution thanks to "Hovercraft Full Of Eels": 解决方案感谢“Hovercraft Full Of Eels”:

private Map<String, String> format = new LinkedHashMap<String,String>();

    public class ToComboBoxModel<String> extends AbstractListModel<String> implements MutableComboBoxModel<String> {

        private String selectedItem;

        @Override
        public Object getSelectedItem() {
            return selectedItem;
        }

        @Override
        public void setSelectedItem(Object anItem) {
            // TODO Auto-generated method stub
            for (java.lang.String str : format.keySet()){
                if (anItem.equals(str))
                {
                    selectedItem=(String) str;
                    break;
                }
            }
        }

        @Override
        public String getElementAt(int index) {         
            List<Entry<String,String>> randAccess = new ArrayList<Entry<String,String>>((Collection<? extends Entry<String, String>>) format.entrySet());

            return randAccess.get(index).getKey();
        }

        @Override
        public int getSize() {
            // TODO Auto-generated method stub
            return format.size();
        }

        @Override
        public void removeElement(Object obj) {
            // TODO Auto-generated method stub

        }

        @Override
        public void removeElementAt(int index) {
            // TODO Auto-generated method stub

        }

        @Override
        public void addElement(String item) {


        }

        @Override
        public void insertElementAt(String item, int index) {
            // TODO Auto-generated method stub

        }
    }

I would create a class that implements the MutableComboBoxModel<T> interface and have it use a TreeMap<T> or some other child of SortedMap<T> as its data nucleus. 我将创建一个实现MutableComboBoxModel<T>接口的类,并使用TreeMap<T>SortedMap<T>其他子项作为其数据核。 I don't think that you'd want to use a HashMap since the model of a JComboBox needs to be ordered and a HashMap is not ordered. 我不认为您想要使用HashMap因为需要对JComboBox的模型进行排序并且不对HashMap进行排序。

The class should also extend the AbstractListModel<E> in order to gain the ListModel's functionality automatically saving you from having to maintain your own EventListenerList and giving you two of the bottom four methods in my list above for free, as well as some fireXXX(...) data change notification methods. 该类还应该扩展AbstractListModel<E>以便获得ListModel的功能,自动保存您不必维护自己的EventListenerList并免费提供上面列表中的四个底部四个方法,以及一些fireXXX(...)数据变更通知方法。

What you must do is create the necessary methods dictated by the MutableComboBoxModel<T> 's interface's API . 你必须做的是创建MutableComboBoxModel<T>的接口API所规定的必要方法。 This would only mean implementing 12 methods, 这只意味着实施12种方法,

  • 4 of MutableComboBoxModel<E> , 4 MutableComboBoxModel<E>
    • void addElement(E item)
    • void insertElementAt(E item, int index)
    • void removeElement(E obj)
    • void removeElementAt(int index)
  • 2 of ComboBoxModel<E> , 2个ComboBoxModel<E>
    • E getSelectedItem()
    • void setSelectedItem(E item)
  • and 4 of ListModel<E> , ListModel<E>
    • void addListDataListener(ListDataListener listener) no need to write -- part of AbstractListModel void addListDataListener(ListDataListener listener)无需编写 - AbstractListModel的一部分
    • E getElementAt(int index)
    • int getSize()
    • void removeListDataListener(ListDataListener listener) no need to write -- part of AbstractListModel void removeListDataListener(ListDataListener listener)无需编写 - AbstractListModel的一部分

This should be do-able, I should think. 我认为这应该是可行的。

Edit: on looking this over some more, I'm having an issue with insertElementAt(...) . 编辑:再看一下这个,我遇到了insertElementAt(...) Since I've advocated a SortedMap as the model's nucleus, you can't add an element arbitrarily into the a position in the Map since it is sorted by its "natural" order. 由于我主张将SortedMap作为模型的核心,因此无法在Map中的某个位置任意添加元素,因为它按照“自然”顺序排序。 This works best with using an ArrayList, Vector or other similar collection as the model's data nucleus. 这最适合使用ArrayList,Vector或其他类似的集合作为模型的数据核。

Edit 2 : Much better to use a LinkedHashMap as per Omid's suggestion. 编辑2 :根据奥米德的建议,使用LinkedHashMap要好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM