• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Swing » JList in Java Swing Example
Next →
← Prev

JList in Java Swing Example

By Dinesh Thakur

A list components allows user to select a single or multiple items from a given list of items by clicking on each. By default, a user can select multiple items, however it is also possible to create a list from which user can choose only a single item.

When the number of items in the list exceeds the available space then the list does not scroll automatically and only the top group of items will be available. In order to make a list scroll, you must insert it into a scrollpane.

The JList class provides the visual representation for selecting items in a set. An object of JList can be initiated with an array or a list of values. The JList object will create one item for each element in the array vector or list. The data in the JList object can be initiated by calling the constructor with the array vector as the parameter or by using the setListData() method. The JList class allows the user to create either a single-selection list (that is, a list in which only one item or element can be selected) or a multiple-selection list (which is a list in which more than one element can be selected.)

A list can be created by instantiating a JList class. Relevant methods:

 

Method

Description

publicJList()

Createsan emptyJList;

publicJList(Object [] listItems)

created withtheitemscontained in thearrayof objects;

publicJList(VectorlistItems)

created withtheVectorelements;

getSelectedIndexpublic int()

returns theindex of the firstselectItem(-1if none);

public int[] getSelectedIndices()

returns an array ofindicesthat are selected;

getSelectedValuepublic Object()

givesthereference to the object;

public Object[] getSelectedValues​​()

returns anarraywith the selectedObject.

 

Program explains the usage of the class Jlist. A scroll pane can also be attached to a Jlist item if the number of elements is more than the size of list.

import javax.swing.*;
import java.awt.*;
class ListBoxExample extends JFrame
{
    ListBoxExample()
    {
        setLayout(new FlowLayout());
        String[] language = {"Cobol","Fortran","Pascal","C","C++","Java","C#"};
        JLabel lblLanguage = new JLabel("Choose the Language");
        JList LstMonth = new JList(language);
        add(lblLanguage);   add(LstMonth);
   
    }
}
  class ListBoxJavaExample
  {
       public static void main(String args[])
      {
            ListBoxExample frame = new ListBoxExample();
            frame.setTitle("ListBox Java Example");
            frame.setBounds(200,250,150,200);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
   }
 }

 

 

JList in Java Swing Example

 One of the another Example for JList is:

// <applet code=ListEx width=250 height=375> </applet> 
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
public class ListEx extends JApplet
{
    String[] bikes = { "Honda Activa", "Honda Aviator", "Honda CB Shine", "Honda CB Trigger", "Honda CB Twister", "Honda CB Unicorn","Honda CB Unicorn Dazzler",
"Honda CBF Stunner","Honda CBR 1000RR","Honda CBR150R","Honda CB1000R","Honda CBR250R","Honda Dio" };
    DefaultListModel ListModel=new DefaultListModel();
    JList list = new JList(ListModel);
    JTextArea textarea = new JTextArea(bikes.length,20);
    JButton button = new JButton("Add More bikes");
    ActionListener Action = new ActionListener()
   {
       public void actionPerformed(ActionEvent e)
      {
                if(count < bikes.length)
                 {
                         ListModel.add(0, bikes[count++]);
                 } else
                     {
                            button.setEnabled(false);
                     }
       }
   };
        ListSelectionListener ListSelection = new ListSelectionListener()
        {
            public void valueChanged(ListSelectionEvent e)
           {
               textarea.setText("");
               Object[] items=list.getSelectedValues();
               for(int i = 0; i < items.length; i++)
                    textarea.append(items[i] + "\n");
            }
        };
            int count = 0;
            public void init()
           {
                   Container cont = getContentPane();
                   textarea.setEditable(false);
                   cont.setLayout(new FlowLayout());
                   Border brd = BorderFactory.createMatteBorder( 1, 1, 2, 2, Color.black);
                   list.setBorder(brd);
                   textarea.setBorder(brd);
                   for(int i = 0; i < 4; i++)
                         ListModel.addElement(bikes[count++]);
                         cont.add(textarea);
                         cont.add(list);
                         cont.add(button);
                         list.addListSelectionListener(ListSelection);
                         button.addActionListener(Action);
            }
           
}

list

You’ll also like:

  1. Java – Add an image to a JList item
  2. JScrollPane in Java Swing Example
  3. Example JScrollPane in Java Swing
  4. showInputDialog Java Swing Example
  5. Sound in Java Swing Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW