fill JComboBox Combo Box - Java Swing

Java examples for Swing:JComboBox

Description

fill JComboBox Combo Box

Demo Code


//package com.java2s;
import java.util.List;

import javax.swing.*;

public class Main {
    public static void fillComboBox(JComboBox combo, List<Object> list) {
        ComboBoxModel model = combo.getModel();
        if (model == null) {
            model = new DefaultComboBoxModel();
            combo.setModel(model);/*w w w  .  ja  v a 2s.c  o m*/
        }
        ;
        if (model instanceof DefaultComboBoxModel) {
            DefaultComboBoxModel listModel = (DefaultComboBoxModel) model;
            listModel.removeAllElements();
            for (int i = 0; i < list.size(); i++) {
                listModel.addElement(list.get(i));
            }
        }
    }
}

Related Tutorials