2

我将一个数组绑定到JComboBox如下:

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

但我希望绑定数组JComboBox动态如下:

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

怎么做?

4

3 回答 3

3

一个有点奇怪的解决方法(我的:)),可能对你有用

final JComboBox lstA = new JComboBox();
String[] arr={"ab","cd","ef"};
lstA.setModel(new JComboBox(arr).getModel());
于 2013-10-06T09:36:27.623 回答
1

使用动态ComboBoxModel构建您的 JComboBox

JComboBox(ComboBoxModel<E> aModel)

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

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

然后您可以添加和删除元素:

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

或者,如果您只需要将数组放入组合框中:

new JComboBox(new Sring[]{"ab","cd","ef"})
于 2013-10-06T09:29:19.053 回答
1
final JComboBox lstA = new JComboBox();
void bind()
{
  String[] arr={"ab","cd","ef"};
  // bind arr to lstA 
  lstA.setModel(new DefaultComboBoxModel<String>(arr));    
}
于 2013-10-06T09:36:11.510 回答