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..
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);
}
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!
来源:https://stackoverflow.com/questions/15333941/loading-an-arraylist-into-a-jcombobox-using-netbeans