Java JList Add Remove fillList(Object[] elements, JList list)

Here you can find the source of fillList(Object[] elements, JList list)

Description

fillList

License

Open Source License

Parameter

Parameter Description
anList List
anJList JList

Declaration

public static void fillList(Object[] elements, JList list) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;

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

    /**
     * fillList
     *
     * @param anList List
     * @param anJList JList
     */
    public static void fillList(Iterable<?> elements, JList list) {
        setListModel(generateListModel(elements), 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

  1. addDoubleClickEvent(JList list)
  2. addItemJList(javax.swing.JList jlist, String item)
  3. addToList(JList list, Object object)
  4. fillList(Collection anList, JList anJList)
  5. fillList(JList aListComponent, String theList, boolean removeQuotes)
  6. JListAddObject(javax.swing.JList list, Object item)
  7. removeAllListItems(JList sourceList)
  8. removeFromList(JList list, Object object)
  9. removeItemJList(javax.swing.JList jlist)