Home > Articles

  • Print
  • + Share This
This chapter is from the book

Lists

The last Swing component to be introduced in this lesson is similar to combo boxes. Lists, which are represented by the JList class, allow you to select one or more values.

You can create and fill lists with the contents of an array or a vector (a data structure similar to array lists). The following constructors are available:

  • JList(): Creates an empty list

  • JList(Object[]): Creates a list that contains an array of the specified class (such as String)

  • JList(Vector<Class>)[: Creates a list that contains the specified java.util.Vector object of the specified class

An empty list can be filled by calling its setListData() method with either an array or a vector as the only argument.

Unlike combo boxes, lists display more than one of their rows when they are presented in a user interface. The default is to display eight items. To change this, call setVisibleRowCount(int) with the number of items to display.

The getSelectedValuesList() method returns a list of objects containing all the items selected in the list. This list can be cast to an ArrayList.

You can use generics with JList to indicate the class of the object array the list contains.

The Subscriptions application in the com.java21days package, shown in Listing 9.7, displays items from an array of strings.

LISTING 9.7 The Full Text of Subscriptions.java

 1: package com.java21days;
 2:
 3: import javax.swing.*;
 4:
 5: public class Subscriptions extends JFrame {
 6:     String[] subs = { "Burningbird", "Freeform Goodness", "Inessential",
 7:         "Manton.org", "Micro Thoughts", "Rasterweb", "Self Made Minds",
 8:         "Whole Lotta Nothing", "Workbench" };
 9:     JList<String> subList = new JList<>(subs);
10:
11:     public Subscriptions() {
12:         super("Subscriptions");
13:         setSize(150, 335);
14:         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15:         JPanel panel = new JPanel();
16:         JLabel subLabel = new JLabel("RSS Subscriptions:");
17:         panel.add(subLabel);
18:         subList.setVisibleRowCount(8);
19:         JScrollPane scroller = new JScrollPane(subList);
20:         panel.add(scroller);
21:         add(panel);
22:         setVisible(true);
23:     }
24:
25:     private static void setLookAndFeel() {
26:         try {
27:             UIManager.setLookAndFeel(
28:                 "javax.swing.plaf.nimbus.NimbusLookAndFeel"
29:             );
30:         } catch (Exception exc) {
31:             System.out.println(exc.getMessage());
32:         }
33:     }
34:
35:     public static void main(String[] arguments) {
36:         Subscriptions.setLookAndFeel();
37:         Subscriptions app = new Subscriptions();
38:     }
39: }

The application is shown in Figure 9.8. The Subscriptions application has an interface with a label atop a list displaying nine items. A JScrollPane is used in lines 19–20 to enable the list to be scrolled to see the list’s final item, which wouldn’t be visible otherwise. It’s a container that causes its contents to have scrollbars so that it doesn’t take up too much space in a user interface.

FIGURE 9.8

FIGURE 9.8 The Subscriptions application.

  • + Share This
  • 🔖 Save To Your Account