2

I binding an array to JComboBox like following:

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

but I want bind array to JComboBox dynamically like following :

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

How to do it?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Samiey Mehdi
  • 9,184
  • 18
  • 49
  • 63

3 Answers3

3

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());
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

build your JComboBox with a dynamic ComboBoxModel

JComboBox(ComboBoxModel<E> aModel)

like http://docs.oracle.com/javase/7/docs/api/javax/swing/DefaultComboBoxModel.html

m=new DefaultComboBoxModel();
j=JComboBox(m);

you can then add and remove elements:

m.addElement("ab")
m.addElement("cd")

or, if you only need to put the array in the combox:

new JComboBox(new Sring[]{"ab","cd","ef"})
Pierre
  • 34,472
  • 31
  • 113
  • 192
1
final JComboBox lstA = new JComboBox();
void bind()
{
  String[] arr={"ab","cd","ef"};
  // bind arr to lstA 
  lstA.setModel(new DefaultComboBoxModel<String>(arr));    
}
Roman C
  • 49,761
  • 33
  • 66
  • 176