fill Collection to JList - Java Swing

Java examples for Swing:JList

Description

fill Collection to JList

Demo Code


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

import javax.swing.*;

public class Main {
    /**//www.j  a  v  a2s.  c  o m
     * fillList
     *
     * @param anList List
     * @param anJList JList
     */
    public static void fillList(Collection anList, JList anJList) {
        DefaultListModel listModel = new DefaultListModel();
        if (anList != null && anJList != null) {
            Object el = null;
            Iterator it = anList.iterator();
            while (it.hasNext()) {
                el = it.next();
                listModel.addElement(el);
            }
        }
        anJList.setModel(listModel);
        if (listModel.size() > 0) {
            anJList.setSelectedIndex(0);
        }
    }
}

Related Tutorials