有没有人有一个如何将 HashMap 的键绑定到 ComboBox 以便 HashMap 中的更改立即反映到 ComboBox 的示例?
谢谢!
编辑:
感谢“充满鳗鱼的气垫船”的解决方案:
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
}
}