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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.