Loading an ArrayList into a JCombobox using netbeans

匿名 (未验证) 提交于 2019-12-03 02:42:02

问题:

Currently have an ArrayList called SundayList which is loaded as soon as the frame AddStudent is loaded (bit of GUI)

The code automatically generated by Netbeans is:

comboboxSunday = new javax.swing.JComboBox();  comboboxSunday.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item1", "Item2" })); 

How do I load the combobox items with my own array? The array includes items such as:

Activity1 Activity2 Activity3 Activity4 

From my previous search, people mentioned about using a toString() and toArray(), and I'm not familiar with either methods, and how they help in loading the list into the combobox as I'm quite new to java..

回答1:

You could create your own ComboBoxModel that takes a List as the main parameter, but that's a little more involved

comboboxSunday.setModel(new DefaultComboBoxModel()); for (Object item : listOfItems) {     comboboxSunday.addItem(item); } 


回答2:

Assuming your array looks something like this:

String[] SundayList = { "Activity1", "Activity2", "Activity3", "Activity4" }; 

You can do this:

javax.swing.JComboBox sundayCombo = new javax.swing.JComboBox(SundayList); 

If your array isn't a string array. try:

javax.swing.JComboBox sundayCombo = new javax.swing.JComboBox(SundayList.toString()); 

Hope this helps!



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!