fill JList List - Java Swing

Java examples for Swing:JList

Description

fill JList List

Demo Code


//package com.java2s;

import javax.swing.*;

public class Main {
    /**//  w  w w . j a  va 2 s .  c  o m
     * fillList
     *
     * @param anList List
     * @param anJList JList
     */
    public static void clearList(JList list) {
        setListModel(generateListModel(), list);
    }

    private static void setListModel(ListModel model, JList list) {
        list.setModel(model);
        if (model.getSize() > 0) {
            list.setSelectedIndex(0);
        }
    }

    private static ListModel generateListModel(Object[] elements) {
        DefaultListModel listModel = new DefaultListModel();
        if (elements != null) {
            for (Object o : elements) {
                listModel.addElement(o);
            }
        }
        return listModel;
    }

    private static DefaultListModel generateListModel(Iterable<?> elements) {
        DefaultListModel listModel = generateListModel();
        if (elements != null) {
            for (Object o : elements) {
                listModel.addElement(o);
            }
        }
        return listModel;
    }

    private static DefaultListModel generateListModel() {
        return new DefaultListModel();
    }
}

Related Tutorials