fill list of item to JComboBox Combo Box - Java Swing

Java examples for Swing:JComboBox

Description

fill list of item to 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 list) {
        ComboBoxModel model = combo.getModel();
        if (model == null) {
            model = new DefaultComboBoxModel();
            combo.setModel(model);//from  ww w.  j ava 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