How To Fill A Combobox Using An ArrayList In Java NetBeans
In This Java Tutorial We Will See How To Get Data From An ArrayList And Set It Into A JComboBox Using Java Programming Language And NetBeans Editor.
Project Source Code:
// create a class "User" to fill the jcombobox
public class User{
public int id;
public String username;
public User(int Id, String Username)
{
this.id = Id;
this.username = Username;
}
}
// populate the arraylist using the class "User"
public ArrayList createList()
{
ArrayList<User> userList = new ArrayList<>();
User u1 = new User(10, "User 1");
User u2 = new User(22, "User 2");
User u3 = new User(36, "User 900");
User u4 = new User(49, "User 4");
User u5 = new User(55, "User 5");
User u6 = new User(61, "User 6");
userList.add(u1);
userList.add(u2);
userList.add(u3);
userList.add(u4);
userList.add(u5);
userList.add(u6);
return userList;
}
// populate the jcombobox using "createList()" function
public void fillCombo()
{
ArrayList<User> list = createList();
for(int i = 0; i < list.size(); i++)
{
jComboBox1.addItem( Integer.toString(list.get(i).id));
}
}
///////////////OUTPUT:
Download Projects Source Code