java – 动态地将项目添加到JComboBox
发布时间:2020-12-14 05:20:27 所属栏目:Java 来源:网络整理
导读:Vector comboBoxItems = new Vector();DefaultComboBoxModel model;// ComboBox Items have gotten from Data Base initially.model = new DefaultComboBoxModel(ComboBoxItems);JComboBox box = new JComboBox(model); 我将此组合框添加到面板.如果我直接在
Vector comboBoxItems = new Vector(); DefaultComboBoxModel model; // ComboBox Items have gotten from Data Base initially. model = new DefaultComboBoxModel(ComboBoxItems); JComboBox box = new JComboBox(model); 我将此组合框添加到面板.如果我直接在数据库中添加一些项目,那么我希望在组合框中显示新添加的项目. 当我调试时,我可以看到comboBoxItem中的值,但这些值不会出现在我的组合框中. 如何将这些新添加的值添加到组合框中而不关闭面板? 解决方法
如何使用ComboBoxModel?这样的东西….
JFrame frame = new JFrame("Combo Box Demo"); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200,200); frame.setLayout(new FlowLayout()); Vector comboBoxItems=new Vector(); comboBoxItems.add("A"); comboBoxItems.add("B"); comboBoxItems.add("C"); comboBoxItems.add("D"); comboBoxItems.add("E"); final DefaultComboBoxModel model = new DefaultComboBoxModel(comboBoxItems); JComboBox comboBox = new JComboBox(model); frame.add(comboBox); JButton button = new JButton("Add new element in combo box"); frame.add(button); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { model.addElement("F"); } }); frame.setVisible(true); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |