Tóm Tắt
JList basic tutorial and examples
- Details
- Written by Nam Ha Minh
-
Last Updated on 06 July 2019   |  
Print
Email
JList is a Swing component with which we can display a list of elements. This component also allows the user to select one or more elements visually. This article shows how to work with JList and proceeds to show some examples.
We will write code to achieve a
JList
output like:
Output
Table of Contents:
1. Developing A Simple JList
2. Adding a Scrollpane
3. Selection Mode
4. Event Handlers
5. Developing a Selection Listener
1. Developing A Simple JList:
Let us now build a GUI with a
JList
. Let us say we want to show a list of countries. As with other
Swing
components, the data for a
JList
is held in a model. This is represented by
ListModel
interface in the
Swing
API. The API provides a default implementation of this class named
DefaultListModel
. More often than not, we would want to display a list of homogeneous
elements.
Using the DefaultListModel:
Let us now see how to create and use the
DefaultListModel
.
Note that, since version 1.7, the API for
JList
allows the ability to create a
JList
with a parameterized type. So, we will use that syntax to create a
JList
that will accept a
List<String>
:
DefaultListModel<String> listModel = new DefaultListModel<>(); listModel.addElement("USA"); listModel.addElement("India"); listModel.addElement("Vietnam"); listModel.addElement("Canada"); listModel.addElement("Denmark"); listModel.addElement("France"); listModel.addElement("Great Britain"); listModel.addElement("Japan");
The preceding piece of code is quite simple. We create an instance of the DefaultListModel class by declaring it as accepting only String values using the parameterized syntax.
Then, we can use this model to create a JList:
JList<String> countryList = new JList<>(listModel);
It is important to note that the JList declaration and usage should also be parameterized.
Running the Sample:
Let us provide the full source code and run it:
package net.codejava.swing; import javax.swing.DefaultListModel; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.SwingUtilities; public class JListExample extends JFrame { private JList<String> countryList; public JListExample() { //create the model and add elements DefaultListModel<String> listModel = new DefaultListModel<>(); listModel.addElement("USA"); listModel.addElement("India"); listModel.addElement("Vietnam"); listModel.addElement("Canada"); listModel.addElement("Denmark"); listModel.addElement("France"); listModel.addElement("Great Britain"); listModel.addElement("Japan"); //create the list countryList = new JList<>(listModel); add(countryList); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("JList Example"); this.setSize(200,200); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new JListExample(); } }); } }
Initial Output
2. Adding a Scrollpane:
Let us now try and resize the frame. We will get the following output:
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on
is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.
Attachments:JListExample.java[Source code for JList basic tutorial and examples]1 kB
JList_example_src.zip[Source code and executable jar file]2 kB