... Selecting colors from a JList.

 1   // Fig. 26.24: ListTest.java
 2   // Selecting colors from a JList.
 3   import javax.swing.JFrame;
 4
 5   public class ListTest
 6   {
 7      public static void main(String[] args)
 8      {
 9         ListFrame listFrame = new ListFrame(); // create ListFrame
10         listFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
11         listFrame.setSize(350, 150);
12         listFrame.setVisible(true);
13      }
14   }

Computer code corresponding outputs.

Line 29 (Fig. 26.23) creates JList object colorJList. The argument to the JList constructor is the array of Objects (in this case Strings) to display in the list. Line 30 uses JList method setVisibleRowCount to determine the number ...

Get Java How to Program, Early Objects, 11th Edition 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.