Working JList And ScrollPane in Java


Introduction of JList

In this article we are going to describe how to use JList and JScrollPane in an application. JList is a component that displays a group of items to the user and allows him/her to select one or more items from the list. A listModel(interface) is used for maintaining the contents of the the list. A class DefaultListModel(inherits ListModel) is used for maintaining a contents list. This class actually provides methods to add removeAll elements or a specific elements list and to find the index of an element. A component that allows the user to select one or more objects from a list. A separate model, ListModel, represents the contents of the list. It's easy to display an array or vector of objects, using a JList constructor that builds a ListModel instance for you. A List doesn't support scrolling directly. To create a scrolling list you make the JList the viewport view of a JScrollPane. By default the JList selection model allows any combination of items to be selected at a time, using the constant MULTIPLE_INTERVAL_SELECTION. The selection state is actually managed by a separate delegate object, an instance of ListSelectionModel. However JList provides convenient properties for managing the selection.

Note - JJList doesn't provide any special support for handling double or triple (or N) mouse clicks however it's easy to handle them using a MouseListener. Use the JList method locationToIndex() to determine what cell was clicked.

Example

import javax.swing.*;

import javax.swing.event.ListSelectionEvent;

import javax.swing.event.ListSelectionListener;

import java.awt.*;

import java.awt.event.*;

public class JListExample extends JFrame

{

JList list;

String[] listColorNames = { "BLACK", "BLUE", "GREEN", "YELLOW","WHITE" };

Color[] listColorValues = { Color.BLACK, Color.BLUE, Color.GREEN,Color.YELLOW, Color.WHITE };

Container contentpane;

public JListExample()

{

super("List Example ");

contentpane = getContentPane();

contentpane.setLayout(new FlowLayout());

list = new JList(listColorNames);

list.setSelectedIndex(0);

list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

contentpane.add(new JScrollPane(list));

list.addListSelectionListener(new ListSelectionListener()

{

public void valueChanged(ListSelectionEvent e)

{

contentpane.setBackground(listColorValues[list.getSelectedIndex()]);

}

});

setSize(300, 300);

setVisible(true);

}

public static void main(String[] args)

{

JListExample test = new JListExample();

test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

Output

Cmd output

ncmd1.jpg

First or initial output.

nimg00.jpg

When you select the BLUE item of the list.

aa.jpg

When you select GREEN item on list.

aa1.jpg

Introduction of JScrollPane

JScollPane manages a viewport, optional vertical and horizontal scroll bars, and optional row and column heading viewports. The JViewport provides a window, or "viewport" onto a data source -- for example, a text file. That data source is the "scrollable client" (aka data model) displayed by the JViewport view. A JScrollPane basically consists of JScrollBars, a JViewport, and the wiring between them, as shown in the following diagram.

JScrollPane-1.gif

Example

import javax.swing.*;

public class JScrollPaneDemo

{

public static void main(String[] args)

{

String st = "Amit Maheswari\n"

+ "Akshay Tewatiya\n"

+ "Praveen Kumar\n"

+ "Vipendra kumar\n"

+ "Arjun Pawar"

+ "sanjoli\n" +"Deepak Dwij"

+ "Amit\n" + "Anuj\n"

+ "Sourabh Tripathi\n" + "Vaibhav Tripathi\n"

+ "Anamika" + "KRIShana\n"

+ "I have four cars and two computers.";

JFrame frame = new JFrame("Example a JScrollPane ");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();

JTextArea area = new JTextArea(st,8,12);

JScrollPane spane = new JScrollPane(area);

panel.add(spane);

frame.add(panel);

frame.setSize(400,400);

frame.setVisible(true);

}

}

Output

ncmd2.jpg

nimg01.jpg

Resources

Describing the Java Packages
Garbage Collector in .NET
Use of ByteStreams and CharacterStreams in JAVA
Describing the JSF Elements
Learning JDBC (Java Database Connectivity) 

Up Next
    Ebook Download
    View all
    Learn
    View all