Binding HashMap to ComboBox
combobox, hashmap, java, swing
Solution
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. 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.
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.
What you must do is create the necessary methods dictated by the `MutableComboBoxModel<T>`'s interface's API. This would only mean implementing 12 methods,
- 4 of `MutableComboBoxModel<E>`,
- `void addElement(E item)`
- `void insertElementAt(E item, int index)`
- `void removeElement(E obj)`
- `void removeElementAt(int index)`
- 2 of `ComboBoxModel<E>`,
- `E getSelectedItem()`
- `void setSelectedItem(E item)`
- and 4 of `ListModel<E>`,
- `void addListDataListener(ListDataListener listener)` no need to write -- part of AbstractListModel
- `E getElementAt(int index)`
- `int getSize()`
- `void removeListDataListener(ListDataListener listener)` no need to write -- part of AbstractListModel
This should be do-able, I should think.
Edit: on looking this over some more, I'm having an issue with `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. This works best with using an ArrayList, Vector or other similar collection as the model's data nucleus.
Edit 2: Much better to use a LinkedHashMap as per Omid's suggestion.
Problem
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? Thanks! EDIT: Solution thanks to "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 } } ```