JList And JScrollPane 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 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
  1. import javax.swing.*;  
  2. import javax.swing.event.ListSelectionEvent;  
  3. import javax.swing.event.ListSelectionListener;  
  4. import java.awt.*;  
  5. import java.awt.event.*;  
  6. public class JListExample extends JFrame  
  7. {  
  8.     JList list;  
  9.     String[] listColorNames = {  
  10.         "BLACK",  
  11.         "BLUE",  
  12.         "GREEN",  
  13.         "YELLOW",  
  14.         "WHITE"  
  15.     };  
  16.     Color[] listColorValues = {  
  17.         Color.BLACK,  
  18.         Color.BLUE,  
  19.         Color.GREEN,  
  20.         Color.YELLOW,  
  21.         Color.WHITE  
  22.     };  
  23.     Container contentpane;  
  24.     public JListExample()  
  25.     {  
  26.         super("List Example ");  
  27.         contentpane = getContentPane();  
  28.         contentpane.setLayout(new FlowLayout());  
  29.         list = new JList(listColorNames);  
  30.         list.setSelectedIndex(0);  
  31.         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  
  32.         contentpane.add(new JScrollPane(list));  
  33.         list.addListSelectionListener(new ListSelectionListener()  
  34.             {  
  35.                 public void valueChanged(ListSelectionEvent e)  
  36.                 {  
  37.                     contentpane.setBackground(listColorValues[list.getSelectedIndex()]);  
  38.                 }  
  39.             });  
  40.         setSize(300300);  
  41.         setVisible(true);  
  42.     }  
  43.     public static void main(String[] args)  
  44.     {  
  45.         JListExample test = new JListExample();  
  46.         test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  47.     }  
  48. }  
Output
 
Cmd output
 
JList in Java
 
First or initial output.
 
JList in Java
 
When you select the BLUE item of the list.
 
JList in Java
 
When you select GREEN item on list.
 
JList in Java
 

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 
 
Example
  1. import javax.swing.*;  
  2. public class JScrollPaneDemo  
  3. {  
  4.     public static void main(String[] args)  
  5.     {  
  6.         String st = "Amit Maheswari\n"  
  7.             +  
  8.             "Akshay Tewatiya\n"  
  9.             +  
  10.             "Praveen Kumar\n"  
  11.             +  
  12.             "Vipendra kumar\n"  
  13.             +  
  14.             "Arjun Pawar"  
  15.             +  
  16.             "sanjoli\n" + "Deepak Dwij"  
  17.             +  
  18.             "Amit\n" + "Anuj\n"  
  19.             +  
  20.             "Sourabh Tripathi\n" + "Vaibhav Tripathi\n"  
  21.             +  
  22.             "Anamika" + "KRIShana\n"  
  23.             +  
  24.             "I have four cars and two computers.";  
  25.         JFrame frame = new JFrame("Example a JScrollPane ");  
  26.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  27.         JPanel panel = new JPanel();  
  28.         JTextArea area = new JTextArea(st, 812);  
  29.         JScrollPane spane = new JScrollPane(area);  
  30.         panel.add(spane);  
  31.         frame.add(panel);  
  32.         frame.setSize(400400);  
  33.         frame.setVisible(true);  
  34.     }  
  35. }  
Output
 
JScrollPane
 
JScrollPane
 
Resources


Similar Articles