Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Creating a JList in Java Swings

In this lesson, you will learn.

  • Working with JList
  • Constructors and Methods of JList
  • Examples

 

What is a JList

 

 

The JList class in Java Swing is used to create a component that displays a list of items, allowing the user to select one or more items.

 

Constructors of JList

Constructor Description
JList() Creates an empty list.
JList(E[] listData) Creates a list that displays the elements in the specified array.
JList(ListModel<E> dataModel) Creates a list that displays elements from the specified data model.

 

Creating a JList

 

Example -1: Creating a Simple JList on JFrame

package jlist;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class JListExample1 extends JFrame {
	JList <String>list;
	DefaultListModel<String> listmodel;

    public JListExample1() {
        // Set the frame properties
        setTitle("JList Example");
        setSize(400, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        // Use ListModel to create another JList
        listmodel = new DefaultListModel<String>();
        listmodel.addElement("India");
        listmodel.addElement("China");
        listmodel.addElement("France");
        listmodel.addElement("Itly");
        listmodel.addElement("Japan");
        listmodel.addElement("United State");
        
        list = new JList<>(listmodel);
        list.setModel(listmodel);
        add(list);

        // Customize the JList
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setFixedCellHeight(25);
        list.setFixedCellWidth(100);
        //Make Frame Visible
        setVisible(true);
    }

    public static void main(String[] args) {
        // Create the frame
        new JListExample1();
    }
}

 

Output

 

 

DefaultListModel is a class that is used to create a default list of items. The items can be added to the default list model by utilizing the addElement() method.

 

Example -2: Creating a Scrollable JList on JFrame

package jlist;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public class JListExample2 extends JFrame {

	public JListExample2() {
		// Set the frame properties
		setTitle("JList Example");
		setSize(400, 250);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//set layout of panel
		JPanel panel = new JPanel();
		add(panel);

		// Use ListModel to create another JList
		List<Integer> mylist= new ArrayList<>();
		for (int i = 2024; i >= 1900; i--) {
			mylist.add(i);
		}

		JList<Integer> jlist = new JList<>(mylist.toArray(new Integer[mylist.size()]));

		//add list to the panel
		panel.add(jlist);

		//Adding Scrollbar to the list
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(jlist);
		jlist.setLayoutOrientation(JList.VERTICAL);
		panel.add(scrollPane);

		// Customize the JList
		jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jlist.setFixedCellHeight(20);
		jlist.setFixedCellWidth(100);

		//Make Frame Visible
		setVisible(true);
	}

	public static void main(String[] args) {
		// Create the frame
		new JListExample2();
	}
}

 

Output

 

 

Summary of Common Methods in JList

Method Description
void clearSelection() Clears the selection, making it empty.
int getFirstVisibleIndex() Returns the index of the first visible cell.
int getFixedCellHeight() Returns the fixed cell height value.
int getFixedCellWidth() Returns the fixed cell width value.
int getLastVisibleIndex() Returns the index of the last visible cell.
ListSelectionModel getSelectionModel() Returns the selection model for the list.
int getMaxSelectionIndex() Returns the largest selected cell index.
int getMinSelectionIndex() Returns the smallest selected cell index.
int getSelectedIndex() Returns the index of the first selected item, or -1 if no item is selected.
int[] getSelectedIndices() Returns an array of all selected indices.
E getSelectedValue() Returns the value of the first selected item, or null if no item is selected.
List<E> getSelectedValuesList() Returns a list of all selected items.
int getSelectionMode() Returns the current selection mode.
boolean isSelectionEmpty() Returns true if no items are selected.
void setCellRenderer(ListCellRenderer<? super E> cellRenderer) Sets the object responsible for drawing list items.
void setFixedCellHeight(int height) Sets a fixed value for the height of every cell.
void setFixedCellWidth(int width) Sets a fixed value for the width of every cell.
void setListData(E[] listData) Sets the list data to be the specified array of items.
void setModel(ListModel<E> model) Sets the data model that the JList will use to obtain the list of items.
void setSelectionMode(int selectionMode) Sets the selection mode.
void setSelectionModel(ListSelectionModel selectionModel) Sets the selection model for the list.
void setSelectedIndex(int index) Sets the selected index to the specified position.
void setSelectedIndices(int[] indices) Selects a set of cells.
void setSelectedValue(E anObject, boolean shouldScroll) Selects the specified object.

 

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review