Binding a JComboBox to a List

Here is a method to bind a list to a combo box.

Hope it will be helpful

public ComboBoxModel cityModel(List<cityDetail>cityList ) {
ComboBoxModel model;

if (cityList == null || cityList.size() <= 0) {
Object[] d = new Object[1];
//d[0] = “None”;
ComboBoxModel mo = new DefaultComboBoxModel(d);
return mo;
}

Object[] days = new Object[cityList.size() + 1];
int i = 0;
try {
ListIterator<CityDetails> lg = cityList.listIterator();
while (lg.hasNext()) {
CityDetails newcus = lg.next();
days[i] = newcus.getCityname();
i = i + 1;
}

days[i] = “Add New”;
} catch (Exception ex) {
System.out.println(“error occured ” + ex.toString());
}
model = new DefaultComboBoxModel(days);
return model;
}
this.cbCity.setModel(cityModel())

//where cbCity is the jcombobox

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s