summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java
blob: e469f2f382d18cd349e5869e74e1f8c5ffba1c5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package bjc.utils.gui;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListModel;

/**
 * Utility class for making JLists and their models.
 *
 * @author ben
 *
 */
public class SimpleJList {
	/**
	 * Create a new JList from a given list.
	 *
	 * @param <E>
	 *                The type of data in the JList
	 *
	 * @param source
	 *                The list to populate the JList with.
	 * @return A JList populated with the elements from ls.
	 */
	public static <E> JList<E> buildFromList(Iterable<E> source) {
		if(source == null) throw new NullPointerException("Source must not be null");

		return new JList<>(buildModel(source));
	}

	/**
	 * Create a new list model from a given list.
	 *
	 * @param <E>
	 *                The type of data in the list model
	 *
	 * @param source
	 *                The list to fill the list model from.
	 * @return A list model populated with the elements from ls.
	 */
	public static <E> ListModel<E> buildModel(Iterable<E> source) {
		if(source == null) throw new NullPointerException("Source must not be null");

		DefaultListModel<E> defaultModel = new DefaultListModel<>();

		source.forEach(defaultModel::addElement);

		return defaultModel;
	}
}