Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Swing JList With String Array

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:

JList and ListSelectionEvent
JList and ListSelectionEvent

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.

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.

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.

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.

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.

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:

8. Youtube Demo – Swing JList

9. Code Reference

8.1 MainEntry.java

8.2 JavaSwingSliderExample.java

Categories: Swing

Tags: , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.