回答
這裏有從字符串數組創建組合框代碼,所有你需要做的是改變你的列表中的數組。 String petStrings = ...;
//Create the combo box, select item at index 4.
//Indices start at 0, so 4 specifies the pig.
JComboBox petList = new JComboBox(petStrings.toArray());
延伸DefaultComboboxModel並創建一個方法,該方法需要一個集合並設置該集合中的項目。使用setModel()
將此自定義模型設置爲組合框的模型。
查看下面的我的答案......考慮到這是未經測試的,僅僅是一個例子。
您需要創建的ComboBoxModel的自定義implmentation像Chandru說, 然後使用setModel()
方法設置的ComboBoxModel您的JComboBox使用((CustomComboBoxModel<String>)jComboBox.getModel()).add(listOfThings);
事情是這樣的添加元素:
import java.util.List;
import javax.swing.ComboBoxModel;
/**
* Custom Implementation of {@code ComboBoxModel} to allow adding a list of
* elements to the list.
*/
public interface CustomComboBoxModel<T> extends ComboBoxModel {
void add(List<T> elementsToAdd);
List<T> getElements();
}
,然後實現接口使用這樣的事情:
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractListModel;
/**
* Default Implementation of CustomComboBoxModel - untested.
*/
public class DefaultCustomComboBoxModel<T> extends AbstractListModel implements CustomComboBoxModel<T> {
List<T> objects;
T selectedObject;
/**
* Constructs an empty DefaultCustomComboBoxModel object.
*/
public DefaultCustomComboBoxModel() {
objects = new ArrayList<T>();
}
/**
* Constructs a DefaultCustomComboBoxModel object initialized with
* an array of objects.
*
* @param items an array of Object objects
*/
public DefaultCustomComboBoxModel(final T items[]) {
objects = new ArrayList<T>();
int i, c;
for (i = 0, c = items.length; i < c; i++) {
objects.add(items[i]);
}
if (getSize() > 0) {
selectedObject = objects.get(0);
}
}
// implements javax.swing.ComboBoxModel
/**
* Set the value of the selected item. The selected item may be null.
* Make sure {@code anObject} is an instance of T otherwise a
* ClassCastException will be thrown.
* <p>
* @param anObject The combo box value or null for no selection.
*/
@Override
public void setSelectedItem(Object anObject) {
if ((selectedObject != null && !selectedObject.equals(anObject))
|| selectedObject == null && anObject != null) {
selectedObject = (T) anObject;
fireContentsChanged(this, -1, -1);
}
}
// implements javax.swing.ComboBoxModel
@Override
public T getSelectedItem() {
return selectedObject;
}
// implements javax.swing.ListModel
@Override
public int getSize() {
return objects.size();
}
// implements javax.swing.ListModel
@Override
public T getElementAt(int index) {
if (index >= 0 && index < objects.size()) {
return objects.get(index);
} else {
return null;
}
}
/**
* Returns the index-position of the specified object in the list.
*
* @param anObject
* @return an int representing the index position, where 0 is
* the first position
*/
public int getIndexOf(T anObject) {
return objects.indexOf(anObject);
}
// implements javax.swing.MutableComboBoxModel
public void addElement(T anObject) {
objects.add(anObject);
fireIntervalAdded(this, objects.size() - 1, objects.size() - 1);
if (objects.size() == 1 && selectedObject == null && anObject != null) {
setSelectedItem(anObject);
}
}
// implements javax.swing.MutableComboBoxModel
public void insertElementAt(T anObject, int index) {
objects.add(index, anObject);
fireIntervalAdded(this, index, index);
}
// implements javax.swing.MutableComboBoxModel
public void removeElementAt(int index) {
if (getElementAt(index) == selectedObject) {
if (index == 0) {
setSelectedItem(getSize() == 1 ? null : getElementAt(index + 1));
} else {
setSelectedItem(getElementAt(index - 1));
}
}
objects.remove(index);
fireIntervalRemoved(this, index, index);
}
// implements javax.swing.MutableComboBoxModel
public void removeElement(T anObject) {
int index = objects.indexOf(anObject);
if (index != -1) {
removeElementAt(index);
}
}
/**
* Empties the list.
*/
public void removeAllElements() {
if (objects.size() > 0) {
int firstIndex = 0;
int lastIndex = objects.size() - 1;
objects.clear();
selectedObject = null;
fireIntervalRemoved(this, firstIndex, lastIndex);
} else {
selectedObject = null;
}
}
@Override
public void add(List<T> elementsToAdd) {
objects.addAll(elementsToAdd);
fireContentsChanged(this, -1, -1);
}
@Override
public List<T> getElements() {
return objects;
}
}
這裏沒有評論,三個提議和回答者說*「考慮到這是未經測試,僅僅是一個例子」* ...這項工作? – Pureferret 2012-07-26 07:34:44
@pureferret我忘記了這個,我會試一試,使用這段代碼把測試用例放在一起,然後回覆你... – edwardsmatt 2012-07-26 08:40:14
我剛剛實現了它,它似乎只有一顆「無類型轉換爲T'或'輸入對象',我想。我現在無法找到它,因爲我壓制了它。 – Pureferret 2012-07-26 08:47:25
最簡單的方法是:
comboBox.setModel(new DefaultComboBoxModel(list.toArray()));
我知道這是一箇舊帖子,但我想對edwardsmatt的DefaultCustomComboBoxModel做一個小的補充。不要忘了添加這個構造函數:
public DefaultCustomComboBoxModel(List<T> list) {
objects = list;
if (getSize() > 0) {
selectedObject = objects.get(0);
}
}
這樣模型也可以用列表來初始化,
myCombo.setModel(new DefaultCustomComboBoxModel(myList));
如果您使用((CustomComboBoxModel)myCombo.getModel()).add(myList)
您需要明確設置所選項目。
你也可以做這樣的:
DefaultTableModel modelTabele = (DefaultTableModel) tblOsobe.getModel();
modelTabele.addColumn("Ime");
modelTabele.addColumn("Prezime");
modelTabele.addColumn("Datum Rodjenja");
for (Osoba osoba : Liste.osobe) {
System.out.println("" + osoba);
Object[] podaci = new Object[3];
podaci[0] = osoba.getIme();
podaci[1] = osoba.getPrezime();
podaci[2] = osoba.getDatumRodjenja();
modelTabele.addRow(podaci);
}
這種模式有3列和多行,因爲在字符串Liste.osobe名單。
- 1. 使用字符串元素列表作爲Select2組件的源
- 2. 如何使用字符串列表作爲列表框的DataSource
- 3. 使用Windows窗體組合框與列表<KeyValuePair <UserEnum,字符串作爲數據源 - C#
- 4. 使用字典作爲數據源的綁定組合框
- 5. n個列表字符串的組合
- 6. 綁定組合框:排序字符串的源列表後文本消失
- 7. 使用字符串數組列表長度作爲整數
- 8. WPF數據綁定到組合框的字符串列表
- 9. 使用字符串作爲分隔符來拆分字符串
- 10. 輸出字符串數組列表框
- 11. 將組合框項綁定到字符串資源數組
- 12. 如何從字符串列表中生成字符串組合?
- 13. 1D數組是作爲字符串列表還是單個字符串工作?
- 14. 如何使列表[字符串]爲字符串列表
- 15. JAVA將字符串轉換爲字符串數組的列表
- 16. ExtJS的字符串組合框
- 17. 更好的方法來元組列表轉換爲字符串
- 18. 使用字符串行作爲列
- 19. 字符串組合不起作用
- 20. 組合使用遞歸的字符串
- 21. 將字符串列表拆分爲字符串列表列表
- 22. MongoDB,通過聚合框架使用組來獲得唯一的字符串
- 23. 用lambda表達式組合字符串
- 24. 如何使用接受字符串作爲組合框名稱的方法來填充JComboBox
- 25. C#獲取單獨的列表框項目作爲字符串
- 26. 將字符串從數組列表字符串轉換爲數組列表字符串
- 27. 將字符串或字符串[]轉換爲使用Groovy列表
- 28. 從字符串返回字典值作爲元組或列表
- 29. Android:如何使用列表中的字符串數組列表
- 30. 將字符串列表轉換爲字符串數組
我想根據其他用戶選擇即時創建組合框列表。因此我需要將它設置爲組合框,因爲我無法在構造函數中進行初始化。因此,我正在尋找一些方法來設置一個列表或數組到框 – 2010-02-21 09:01:05
所以我認爲你可以使用DefaultComboBoxModel類,它提供了添加和刪除元素以及獲取選定元素的簡便方法。然後通過setModel方法將此模型設置爲JComboBox。 – kokosing 2010-02-21 09:07:17