In this tutorial you will learn about the Swing JList and its application with practical example.
Swing JList
It is a component which displays list of item and allows users to select one or more items. It is easy to display an array or Vector objects , using the JList constructor that automatically build a read-only instance for you. Let’s simply understand it by below simple code. In below code explained that how can we create jList and can add array and vector data in JList.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Create a JList that displays strings from an array String[] data = {"one", "two", "three", "four"}; JList<String> myList = new JList<String>(data); // Create a JList that displays the superclasses of JList.class, by // creating it with a Vector populated with this data Vector<Class<?>> superClasses = new Vector<Class<?>>(); Class<JList> rootClass = javax.swing.JList.class; for(Class<?> cls = rootClass; cls != null; cls = cls.getSuperclass()) { superClasses.addElement(cls); } JList<Class<?>> myList = new JList<Class<?>>(superClasses); // The automatically created model is stored in JList's "model" // property, which you can retrieve ListModel<Class<?>> model = myList.getModel(); for(int i = 0; i < model.getSize(); i++) { System.out.println(model.getElementAt(i)); } |
JList Class Declaration
1 |
public class JList<E> extends JComponent implements Scrollable , Accessible |
JList Constructor
Let’s get more understand about JComboBox, Some of important constructor are listed below.
JList () : This constructor construct a new JList with an empty, read-only model.
JList (ListModel<E> dataModel): This constructor is used to construct a JList that displays the elements from the specified, not-null , model.
JList (E[] listData): This constructor is used to construct a JList in which displays the elements in the specified array.
JComboBox(Vector<? eExtends w> listData): This constructor is used to construct a JList that displays elements in the specified vector.
JList Methos
Some of commonly used methods are listed below. It will help to know more about JList.
void addListSelectionListener(ListSelectionListener listener): This method is used to add a listener to the list, which notified each time a change to the selection occurs.
void clearSelection(): This method is used to clear the selection.
int getFirstVisibleIndex(): It returns an integer value of smallest list index that is currently visible.
ListModel<E> getModel(): This method is used to returns the displayed items of JListComponent which holds by the data model.
int getSelectedIndex(): This method is used to get the smallest selected cell index, and when single item selected in the list.
E getSelectedValue(): This method returns the value of smallest selected cell index .
List<E> getSelectedValuesList(): This method is used to returns the list of selected items.
String getToolTipText(MouseEvent event): This method is used to returns tool tip text.
void setListData(Vector<? extends E> listData): This method is used to create a read-only ListModel from a Vector.
void setListData(E[] listData): This method is used to create a read-only ListModel from an array of items..
void setSelectedIndex(int index): This method used to selects the single cell.
void setSelectionBackground(Color selectionBackground): This method is used to sets the color used to draw the background of selected items.
Example :-
An simple example to get understand about JList. Hopefully this will work for you.
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 |
package w3addaswing; import javax.swing.DefaultListModel; import javax.swing.JFrame; import javax.swing.JList; public class W3ADDASwing { W3ADDASwing() { JFrame frame = new JFrame("JList Example W3Adda"); DefaultListModel<String> list_1 = new DefaultListModel<>(); list_1.addElement("Java"); list_1.addElement("Python"); list_1.addElement("Android"); list_1.addElement("PHP"); JList<String> list = new JList<>(list_1); list.setBounds(100,100, 75,75); frame.add(list); frame.setSize(400,400); frame.setLayout(null); frame.setVisible(true); } public static void main(String[] args) { // TODO code application logic here new W3ADDASwing(); } } |
Output :-
Output of this code will look like below image. You just need to copy this code and paste it in you program.