1  import java.awt.*;
  2  import java.awt.event.*;
  3  import javax.swing.*;
  4  import javax.swing.event.*;
  5  
  6  public class ListPropertiesDemo extends JApplet {
  7    private JList<String> jlst = new JList<String>(new String[] {
  8      "Item1", "Item2", "Item3", "Item4", "Item5", "Item6"});
  9    private JSpinner jspVisibleRowCount =
 10      new JSpinner(new SpinnerNumberModel(8, -1, 20, 1));
 11    private JComboBox jcboLayoutOrientation =
 12      new JComboBox(new String[] {"VERTICAL (0)",
 13      "VERTICAL_WRAP (1)", "HORIZONTAL_WRAP (2)"});
 14    private JComboBox jcboSelectionMode =
 15      new JComboBox(new String[] {"SINGLE_SELECTION (0)",
 16      "SINGLE_INTERVAL_SELECTION (1)",
 17      "MULTIPLE_INTERVAL_SELECTION (2)"});
 18    private JLabel jlblStatus = new JLabel();
 19  
 20    /** Construct the applet */
 21    public ListPropertiesDemo() {
 22      // Place labels in a panel
 23      JPanel panel1 = new JPanel();
 24      panel1.setLayout(new GridLayout(3, 1));
 25      panel1.add(new JLabel("visibleRowCount"));
 26      panel1.add(new JLabel("layoutOrientation"));
 27      panel1.add(new JLabel("selectionMode"));
 28  
 29      // Place text fields in a panel
 30      JPanel panel2 = new JPanel();
 31      panel2.setLayout(new GridLayout(3, 1));
 32      panel2.add(jspVisibleRowCount);
 33      panel2.add(jcboLayoutOrientation);
 34      panel2.add(jcboSelectionMode);
 35  
 36      // Place panel1 and panel2
 37      JPanel panel3 = new JPanel();
 38      panel3.setLayout(new BorderLayout(5, 5));
 39      panel3.add(panel1, BorderLayout.WEST);
 40      panel3.add(panel2, BorderLayout.CENTER);
 41  
 42      // Place elements in the applet
 43      add(panel3, BorderLayout.NORTH);
 44      add(new JScrollPane(jlst), BorderLayout.CENTER);
 45      add(jlblStatus, BorderLayout.SOUTH);
 46  
 47      // Set initial property values
 48      jlst.setFixedCellWidth(50);
 49      jlst.setFixedCellHeight(20);
 50      jlst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 51  
 52      // Register listeners
 53      jspVisibleRowCount.addChangeListener(new ChangeListener() {
 54        @Override
 55        public void stateChanged(ChangeEvent e) {
 56          jlst.setVisibleRowCount(
 57            ((Integer)jspVisibleRowCount.getValue()).intValue());
 58        }
 59      });
 60  
 61      jcboLayoutOrientation.addActionListener(new ActionListener() {
 62        @Override
 63        public void actionPerformed(ActionEvent e) {
 64          jlst.setLayoutOrientation(
 65            jcboLayoutOrientation.getSelectedIndex());
 66        }
 67      });
 68  
 69      jcboSelectionMode.addActionListener(new ActionListener() {
 70        @Override
 71        public void actionPerformed(ActionEvent e) {
 72          jlst.setSelectionMode(
 73            jcboSelectionMode.getSelectedIndex());
 74        }
 75      });
 76  
 77      jlst.addListSelectionListener(new ListSelectionListener() {
 78        @Override
 79        public void valueChanged(ListSelectionEvent e) {
 80          Object[] values = jlst.getSelectedValues();
 81          String display = "";
 82  
 83          for (int i = 0; i < values.length; i++) {
 84            display += (String)values[i] + " ";
 85          }
 86  
 87          jlblStatus.setText(display);
 88        }
 89      });
 90    }
 91  
 92    public static void main(String[] args) {
 93      ListPropertiesDemo applet = new ListPropertiesDemo();
 94      JFrame frame = new JFrame();
 95      //EXIT_ON_CLOSE == 3
 96      frame.setDefaultCloseOperation(3);
 97      frame.setTitle("ListPropertiesDemo");
 98      frame.getContentPane().add(applet, BorderLayout.CENTER);
 99      applet.init();
100      applet.start();
101      frame.setSize(400, 320);
102      frame.setLocationRelativeTo(null);
103      frame.setVisible(true);
104    }
105  }