O'Reilly logo

Java™ Design Patterns: A Tutorial by James W. Cooper

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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"); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required