我将数组绑定到
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
}
如何做?
一个有点奇怪的解决方法(我的:)),可能对你有用
final JComboBox lstA = new JComboBox();
String[] arr={"ab","cd","ef"};
lstA.setModel(new JComboBox(arr).getModel());
使用动态 ComboBoxModel
构建 JComboBoxJComboBox(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"})
final JComboBox lstA = new JComboBox();
void bind()
{
String[] arr={"ab","cd","ef"};
// bind arr to lstA
lstA.setModel(new DefaultComboBoxModel<String>(arr));
}