1. Introduction to JList Component
The JList is a swing component which displays a list of objects. When number objects are high, one should use the scrolling for the JList so that all the objects can be viewed. The User can select one or more elements from the list. JList component will raise the ListSelectionEvent while there is a change in the item selection.
2. About JList Example
The below screen shows the example:

In the top part of the JFrame, there is a JList which has scrolling enabled. It can show four items with no scrolling. But, to see other items scrolling is required. When the user selects an item, this sample shows that in a label. The label is next to the JList and the above screen not showing it as we have selected no item yet.
There is a set of controls below the JList which we can use to find a specific item in the JList. For example, the user can type Mango in the JTextField and click on the Find button. Our sample will search the item in JList and when it finds one, it highlights that item by selecting it. We will also scroll our list box to the selected item so that user does not need to scroll the List to see what item got selected. This is useful when the list is huge.
3. Jlist Data – String Array
The FlowLayout manages our JFrame. We declare two members in our class. One is JLabel
and other one is JTextField
to get user input for finding an item in the
JList. The string array Fruits holds 8 items which we will use while creating the JList. The java swing list offers more functionality when we use the data model designed for it. But, for a basic JList functionality, an array of object serves the purpose.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class JListExample extends JFrame { //Sample 02: Create a Label JLabel label = new JLabel(); private JTextField txtItemToFind; public JListExample(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 400, 300); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 03: Create List of Fruit Items String[] Fruits = new String[8]; Fruits[0] = "Apple"; Fruits[1] = "Mango"; Fruits[2] = "Banana"; Fruits[3] = "Grapes"; Fruits[4] = "Cherry"; Fruits[5] = "Lemon"; Fruits[6] = "Orange"; Fruits[7] = "Strawberry"; |
4. Create and Display Swing JList
Below code creates the JList with a string array formed in the past section. Our Java Swing List displays four items and remaining can be seen by scrolling the list. We setup this visibility of four items via the method call setVisibleRowCount. Now JList is ready with the data.
1 2 3 |
//Sample 04: Create JList to Show Fruit Name JList ListFruits = new JList(Fruits); ListFruits.setVisibleRowCount(4); |
Next, we need to provide scrolling support. For this we give our ListFruits
instance to the
JScrollPane’s constructor. Then, we add our fruit list component and the label to the frame window.
1 2 3 4 |
//Sample 05: Hand-Over the JList to ScrollPane & Display JScrollPane jcp = new JScrollPane(ListFruits); ControlHost.add(jcp); ControlHost.add(label); |
5. Handle ListSelectionEvent – Selected String
Java Swing’s JList
will raise the
ListSelectionEvent when the user selects an item from it or if they change the selection. One can get the selected item at anytime from the JList. But, if we need to get the selected index as soon as user making the selection, then one should provide the handler method to handle
ListSelectionEvent.
1 2 3 4 5 6 7 8 |
//Sample 06: Handle the JList Event ListFruits.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { String SelectedFruit = (String) ListFruits.getSelectedValue(); label.setText(SelectedFruit); } }); |
In the above code, we provide an anonymous handler to report the selected item. We handle, valueChanged handler method and make a call to the getSelectedValue. The method returns only one item since we are using JList to allow selection of only one item. Recall, JList can store objects as item, and we know that we are storing the string as the data. So, we type cast the return value as string and display that in the label.
6. Adding Search Panel
In the below code, we prepare the panel which hosts the controls required for searching a specific item in the JList. First section of the code snippet prepares the label and adds it to the panel. In the next section, we add the JTextField
and here user feeds the item they want to search. The last section of the code creates a JButton
and adds that to the panel. We add the panel of these three components to the FlowLayout managed JFrame.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//Sample 07: Add Search Panel JPanel findPanel = new JPanel(); JLabel lblNewLabel = new JLabel("Find Item :"); lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 14)); findPanel.add(lblNewLabel); txtItemToFind = new JTextField(); findPanel.add(txtItemToFind); txtItemToFind.setColumns(15); JButton btnFind = new JButton("Find"); findPanel.add(btnFind); add(findPanel); |
7. Scroll to JList Selected Item
We should scan our JList and search for the item given in the JTextField
when the user clicks the Find button in the search panel. When the item is found, we will select the item from the JList. It will be annoying for the user when there are multiple list entries. They need to scroll through the item to see which one is selected. So, we will do the scrolling as well so that JList will show the selected item without asking the user to scroll to the selection. The
ensureIndexVisible method takes an index and asks the JScrollPane
to scroll the item. Below is the handler:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//Sample 08: Find the List Item btnFind.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //Get the find Index and Scroll to that Index String findItem = txtItemToFind.getText(); for (int i = 0; i<Fruits.length; i++) { if (findItem.compareToIgnoreCase(Fruits[i]) == 0) { ListFruits.setSelectedIndex(i); ListFruits.ensureIndexIsVisible(i); } } } }); |
8. Youtube Demo – Swing JList
9. Code Reference
8.1 MainEntry.java
1 2 3 4 5 6 7 8 9 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { //Sample 07: Create Instance of JFrameDemo JListExample frame = new JListExample("JList and ListSelectionEvent Example"); frame.setVisible(true); } } |
8.2 JavaSwingSliderExample.java
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
package tube.coding.examples; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Font; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class JListExample extends JFrame { //Sample 02: Create a Label JLabel label = new JLabel(); private JTextField txtItemToFind; public JListExample(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 400, 300); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 03: Create List of Fruit Items String[] Fruits = new String[8]; Fruits[0] = "Apple"; Fruits[1] = "Mango"; Fruits[2] = "Banana"; Fruits[3] = "Grapes"; Fruits[4] = "Cherry"; Fruits[5] = "Lemon"; Fruits[6] = "Orange"; Fruits[7] = "Strawberry"; //Sample 04: Create JList to Show Fruit Name JList ListFruits = new JList(Fruits); ListFruits.setVisibleRowCount(4); //Sample 05: Hand-Over the JList to ScrollPane & Display JScrollPane jcp = new JScrollPane(ListFruits); ControlHost.add(jcp); ControlHost.add(label); //Sample 06: Handle the JList Event ListFruits.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { String SelectedFruit = (String) ListFruits.getSelectedValue(); label.setText(SelectedFruit); } }); //Sample 07: Add Search Panel JPanel findPanel = new JPanel(); JLabel lblNewLabel = new JLabel("Find Item :"); lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 14)); findPanel.add(lblNewLabel); txtItemToFind = new JTextField(); findPanel.add(txtItemToFind); txtItemToFind.setColumns(15); JButton btnFind = new JButton("Find"); findPanel.add(btnFind); add(findPanel); //Sample 08: Find the List Item btnFind.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //Get the find Index and Scroll to that Index String findItem = txtItemToFind.getText(); for (int i = 0; i<Fruits.length; i++) { if (findItem.compareToIgnoreCase(Fruits[i]) == 0) { ListFruits.setSelectedIndex(i); ListFruits.ensureIndexIsVisible(i); } } } }); } } |
Categories: Swing
Tags: ensureIndexVisible, getSelectedValue, JScrollPane, ListSelectionEvent, setVisibleRowCount, valueChanged