Chapter 31. The JList Class

The JList class is a more powerful replacement for the AWT's List class. A JList can be instantiated using a Vector or an array to represent its contents. It does not support scrolling and thus must be added to a JScrollPane to allow scrolling to take place.

In the simplest program that you can write using a JList, you

  1. add a JScrollPane to the Frame,

  2. create a Vector of data,

  3. create a JList using the Vector, and

  4. add the JList to the JScrollPane's viewport.

This program is shown next.

 JPanel jp = new JPanel(); //a panel in Frame getContentPane().add(jp); //create a scroll pane JScrollPane sp = new JScrollPane(); jp.add(sp); //add the pane to the layout Vector dlist = new Vector(); //create a Vector dlist.addElement("Anchovies"); ...

Get Java™ Design Patterns: A Tutorial now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.