Question

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?

Was it helpful?

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());

OTHER TIPS

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"})
final JComboBox lstA = new JComboBox();
void bind()
{
  String[] arr={"ab","cd","ef"};
  // bind arr to lstA 
  lstA.setModel(new DefaultComboBoxModel<String>(arr));    
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top