Dynamically change JComboBox

前端 未结 4 508
天命终不由人
天命终不由人 2020-12-15 17:32

I am fetching the data values from the database successfully. I have also stored them into a String[] array. I need to load the String array as the items of the

4条回答
  •  天命终不由人
    2020-12-15 17:42

    This is the demo for illustrating default combo box model

    public class ComboPanel extends JPanel {
    
        JComboBox jcbo;
        // this is constructor
        public ComboPanel(ArrayList items) {
            jcbo = new JComboBox();
            // getting exiting combo box model
            DefaultComboBoxModel model = (DefaultComboBoxModel) jcbo.getModel();
            // removing old data
            model.removeAllElements();
    
            for (String item : items) {
                model.addElement(item);
            }
    
            // setting model with new data
            jcbo.setModel(model);
            // adding combobox to panel
            this.add(jcbo);    
        }
    }
    

    I hope this will help little :)

提交回复
热议问题