版权声明:感谢观看我的博客https://me.csdn.net/weixin_43794314 https://blog.csdn.net/weixin_43794314/article/details/85010868
先写一个JComboBox模型,如下:
public class MyComboBox extends AbstractListModel<String> implements ComboBoxModel<String> {
/* 根据索引返回值 */
public String getElementAt(int index) {
return test[index];
}
/* 返回下拉列表框中项目的数目 */
public int getSize() {
return test.length;
}
/* 获取下拉列表框项目 */
public void setSelectedItem(Object item) {
selectedItem = (String) item;
}
/* 获取下拉列表框中的项目 */
public Object getSelectedItem() {
return selectedItem;
}
/* */
public int getIndex() {
for (int i = 0; i < test.length; i++) {
if (test[i].equals(getSelectedItem()))
return i;
}
return 0;
}
String selectedItem = " 请选择性别 ";
String[] test = { "男", "女"};
}
再建一个JComboBox组件,如下:
JComboBox<String> jc = new JComboBox<>(new MyComboBox());
上面的类型是String也可以是其他类型,结合JComboBox组件的方法,再加上数据库操作,即可达到动态的效果。