Reputation: 15
I have an ArrayList
that is getting objects that contain user information from a file
once the load is complete, I need the name property from each object to be loaded into a JComboBox
, so that the user can choose the username to continue.
this conversion from an ArrayList<object> user --> String[] strName
is where I'm having trouble
any help would be greately appreciated!
Upvotes: 0
Views: 446
Reputation: 44821
There's a good tutorial on how to work with combo boxes here.
ArrayList<User> users;
int nUsers = users.size();
String[] userNames = new String[nUsers];
for (int i=0;i <nUsers; ++i) {
User user = users.get(i);
userNames[i] = user.getName();
}
JComboBox userList = new JComboBox(userNames);
If the list is ArrayList<Object> then you'll need to either:
// call toString on the object...
userNames[i] = String.valueOf(user);
// or cast it if you know the type
User user = (User)users.get(i);
Upvotes: 0
Reputation: 347184
String[] values = list.toArray(new String[list.size()]);
You could just as easily loop through the ArrayList
and use the DefaultComboBoxModel's
addElement
method
Upvotes: 2