CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > How to use JList in Java Swing

Swing Tutorial

  • What Is Java Swing
  • First Program
  • What is AWT
  • Advantages of Swing Over AWT
  • JFrame
  • Swing Components
  • JLabel
  • JTextField
  • JPasswordField
  • JButton
  • Event Handling
  • FlowLayout
  • BorderLayout
  • GridLayout
  • GridBagLayout
  • BoxLayout
  • JPanel
  • JCheckBox
  • JRadioButton
  • Border Component
  • JComboBox
  • JList
  • JList with MVC
  • Mouse Event
  • Key Event
  • JMenuBar
  • JTextArea
  • Using Color
  • Use Font
  • Display Image/Icon
  • Dialog Box
  • JTable
  • JDesktopPane and JInternalFrame
  • JDesktopPane
  • JInternalFrame
  • Adapter Classes
  • Swing Timer
  • JScrollPane
  • JSlider
  • JProgressBar
  • JSeparator
  • JToolBar
  • ActionListener
  • ItemListener
  • Worker Thread
  • Nimbus Look and Feel

How to use JList in Java Swing

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 2 min read · 0 Comments
Share: in X

JList is a Swing component in Java that allows you to display a list of items in a graphical user interface.

Following is a basic example of how to use JList in Java Swing:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class JListDemo extends JFrame {

	public JListDemo() {

		setLayout(new FlowLayout());
		
		JButton displayButton = new JButton("Display");
		add(displayButton);
		
		
		// Create an array of items to display in the JList
		String[] items = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };

		// Create a new JList and add it to a JScrollPane
		JList<String> myList = new JList<String>(items);
		myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		myList.setLayoutOrientation(JList.VERTICAL);
		myList.setVisibleRowCount(3);
		
		JScrollPane scrollPane = new JScrollPane(myList);

		// Add the JScrollPane to the frame
		add(scrollPane);

		// Set frame properties and show it
		setTitle("JList Demo");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300, 200);
		setLocationRelativeTo(null);
		setVisible(true);
		
		displayButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("Your selected value is: "+myList.getSelectedValue());
			}
		});
		
	}

	public static void main(String[] args) {
		new JListDemo();
	}

}

Output:

How to use Jlist in Java swing

When, user selects the item from the list and clicks the display button it prints the selected item’s value in the console.

Related Posts:

  • Use JList with MVC Pattern
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Arrays in Java
  • How to Convert JSON Array to Java List (With Examples)
  • Arrays of Primitive Types in Java
  • Array Operator in MongoDB
Tags:javaswing
Was this article helpful?
← Previous ArticleWrapper Classes in Java
Next Article →java.util package

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap