[英]Why won't JComboBox display Strings from custom model?
我為JCombobox創建了一個自定義模型,以使其與LinkedHashMap同步。 我想創建一個可以與任何LinkedHashMap重用的模型(因此可以使用泛型)。 當我用我的應用程序運行它時,字符串無法出現。 當我單擊“字符串”應位於的位置時,動作偵聽器將正確觸發,並且所選項目會更改。 但是,下拉菜單不顯示字符串? 我知道它是基於JList的,所以我需要對JLabel等進行任何處理嗎? 這也是模型的一部分嗎? 我還需要執行其他任何操作嗎?
這是我的自定義模型
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import javax.swing.AbstractListModel;
import javax.swing.MutableComboBoxModel;
/**
* A model design to automatically update based of a the key values of a Hashmap
* @author Skylion
*
* @param <K> The generic representing key of the Hashmap
* @param <V> The generic representing the value of the Hashmap
*/
public class HashMapComboBoxModel<K, V> extends AbstractListModel<K> implements MutableComboBoxModel<K>,
Serializable {
/**
* Auto-generated
*/
private static final long serialVersionUID = -1293826656458176439L;
/**
* The Selected Item
*/
private K selectedItem;
LinkedHashMap<K,V> data;
public HashMapComboBoxModel(LinkedHashMap<K,V> data){
this.data = data;
}
@Override
public K getElementAt(int index) {
List<Entry<K,V>> randAccess = new ArrayList<Entry<K,V>>((Collection<? extends Entry<K, V>>) data.entrySet());
return randAccess.get(index).getKey();
}
@Override
public int getSize() {
return data.size();
}
@Override
public K getSelectedItem() {
return selectedItem;
}
@SuppressWarnings("unchecked")//Equals() implements the check
@Override
public void setSelectedItem(Object anItem) {
for(K keys: data.keySet()){
if(keys.equals(anItem)){
this.selectedItem = (K) anItem;
return;
}
}
}
@Override
public void addElement(Object obj) {
addElement(downcastToEntry(obj));
}
@SuppressWarnings("unchecked")
private Entry<K,V> downcastToEntry(Object obj){
if(obj instanceof Entry && obj.getClass().isAssignableFrom(
data.entrySet().iterator().next().getClass())){
return (Entry<K,V>)obj;
}
return null;
}
/**
* Adds an Entry value to the hashmap
* @param obj The Entry value you want to add
* @return return true if added false otherwise
*/
public boolean addElement(Entry<K,V> obj){
if(obj == null){return false;}
return this.data.entrySet().add(obj);
}
@Override
public void insertElementAt(Object obj, int index) {
Entry<K,V> entry = downcastToEntry(obj);
addToMapAtIndex(index, entry.getKey(), entry.getValue());
}
private void addToMapAtIndex(int index, K key, V value) {
assert (data != null);
assert !data.containsKey(key);
assert (index >= 0) && (index < data.size());
int i = 0;
List<Entry<K, V>> rest = new ArrayList<Entry<K, V>>();
for (Entry<K, V> entry : data.entrySet()) {
if (i++ >= index) {
rest.add(entry);
}
}
data.put(key, value);
for (int j = 0; j < rest.size(); j++) {
Entry<K, V> entry = rest.get(j);
data.remove(entry.getKey());
data.put(entry.getKey(), entry.getValue());
}
}
@Override
public void removeElement(Object obj) {
data.remove(obj);
}
@Override
public void removeElementAt(int index) {
data.remove(getElementAt(index));
}
}
這是我構造JCombobox的方法。
LinkedHashMap<String, myCustomClass> map = new LinkedHashMap<String, myCustomClass();
//代碼...代碼...更多代碼...
JCombobox box = new JCombobox<String>(new HashMapComboBoxModel<String,myCustomClass>(map));
//稍后在代碼中
map.put("myString", myObject);
// JCombobox更新,但下拉菜單變白。
NVM,我知道了。 我需要調用Fire的DataListener方法。 查看JCombobox的源代碼后,我需要通過模型使用add方法對其進行正確更新。 我決定不重寫類,而只是簡單地在getSize()方法上觸發偵聽器,作為一點小技巧。 我強制在調用該方法時更新整個列表,以便在組合框執行for循環時,它會自動更新模型。
它可能很hack,但是可以用!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.