javaarraysswingjcomboboxcomboboxmodel

How to bind an array to JComboBox dynamically?


I binding an array to JComboBox like the following:

String[] arr={"ab","cd","ef"};
final JComboBox lstA = new JComboBox(arr);

but, I want to bind an array to JComboBox dynamically, like the following:

final JComboBox lstA = new JComboBox();
void bind()
{
    String[] arr={"ab","cd","ef"};
    // bind arr to lstA     
}

How to do it?


Solution

  • A little odd workaround(mine :)), might useful to you

    final JComboBox lstA = new JComboBox();
    String[] arr={"ab","cd","ef"};
    lstA.setModel(new JComboBox(arr).getModel());