如何正确使用与JComboBox的一个数组? [英] How do I properly use an array with JComboBox?
问题描述
我一直运行到我的code关于实施ArrayList的到的JComboBox的几个问题。我能够通过使用像roomList.addItem(将selectedItem)code这样做的。
没有一个数组我试图做到的是用一个ArrayList发送新信息的GUI的另一部分,这样我可以使用数组来保存和加载数据时,只要用户启动GUI中的问题:?如何正确使用与JComboBox的一个数组我看到了一些教程,但我似乎无法得到的组合框数组中的元素更新我犯了一个错误的地方,如果你能帮助我将非常感激。)
I have been running into a few issues with my code regarding the implementation of a ArrayList into a JComboBox. I am capable of doing it by using code like "roomList.addItem(selectedItem);
WITHOUT an array. What I am trying to accomplish is to use an arrayList to send new info to another part of the GUI so that I can use that array to save and load data whenever the user starts the GUI. The Question: How do I properly use an array with JComboBox? I have seen a few tutorials, but I just can't seem to get the ComboBox to update with elements in the array. I am making a mistake somewhere, if you could help I would be really grateful . :)
//这片code的距离主要的Java文件;阵列处于OrderSystem.java。
//This piece of code is from the main java file; array is in OrderSystem.java.
OrderSystem order = new OrderSystem();
ArrayList<String> list = order.getArrayList();
//检索来自JComboBox的(不同的)选择的数据,并将其添加到阵列列表
//retrieves the data selected from the JComboBox(different one) and adds it to the array list.
JComboBox<String> test = room.getRoomType();
String selectedItem = (String) test.getSelectedItem();
list.add(selectedItem);
现在,在里面OrderSystem.Java:
创建的ArrayList和JComboBox
Now, inside the OrderSystem.Java: creates a ArrayList and JComboBox
ArrayList<String> list = new ArrayList<String>();
JComboBox<Object> roomList;
...
setComboBox(new JComboBox<>(list.toArray()));
// getter和setter方法
//setters and getters
public ArrayList<String> getArrayList(){
return list;
}
public void setArrayList(ArrayList<String> list){
this.list = list;
}
谢谢!
推荐答案
试试这个:
comboBox.setModel(new DefaultComboBoxModel<>(list.toArray(new String[list.size()])));
这篇关于如何正确使用与JComboBox的一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!