import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.io.*;
import java.util.*;

/** DemoListMultiple - program to demonstrate <code>JList</code> --
* showing multiple item selection.<p>
*
* By default, a <code>JList</code> allows multiple items to be selected.
* An array of indices of the selected items is retrieved using the
* <code>getSelectedIndices</code> method.<p>
*
* In the program we fill a <code>JList</code> with the names of
* all files in the current directory.  The filenames are retrieved
* as follows:<p>
*
* <pre>
*     private String[] filenameArray;
*     ...
*     File f = new File(".");
*     filenameArray = f.list();
* </pre>
* 
* They are placed in a <code>JList</code> as follows:<p>
*
* <pre>
*     private JList fileList;
*     ...
*     fileList = new JList(filenameArray);
*     fileList.setSelectedIndex(0);
* </pre>
*
* A "Display" button is used to display the selected items in a
* text area.  Since buttons trigger action events, the act
* of displaying the selected items is performed in the
* <code>actionPerformed</code> method.  Here's the code:<p>
*
* <pre>
*     int[] n = fileList.getSelectedIndices();
*     String s = "";
*     for (int i = 0; i < n.length; ++i)
*        s += filenameArray[n[i]] + "\n";
*     selectedList.setText(s);
* </pre>
*
* Select/deselect actions on items in the <code>JList</code>
* trigger <i>list selection</i> events.  For each such event, a simple
* message indicating the number of selected items
* is sent to the console:<p>
*
* <pre>
*     int[] n = fileList.getSelectedIndices();
*     System.out.println(n.length + " item(s) selected"); 
* </pre>
*
* The output to the console provides a convenient means to study
* the generation of list selection events.  When an item is selected
* by a mouse click, two list selection events occur, one on button down,
* one on button up.  Navigating the list with keys such as up-arrow,
* down-arrow, PgUp, or PgDown also triggers list selection events.
* Multiple items may be selected by holding down the CONTROL or SHIFT
* modifier keys.
* <p>
*
* Since both the list of filenames and the display of
* selected items may be lengthy, both the <code>JList</code>
* and the <code>JTextArea</code> are placed in <code>JScrollPane</code>
* objects.  Scrollbars appear automatically, if necessary.<p>
*
* Screen snap...<br>
* <center><img src="DemoListMultiple-1.gif"></center><p>
*
* @see <a href="DemoListMultiple.java">source code</a>
* @author Scott MacKenzie, 2002
*/
public class DemoListMultiple
{
   public static void main(String[] args)
   {
      DemoListMultipleFrame frame = new DemoListMultipleFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("DemoListMultiple");
      frame.pack();
      frame.show();
   }
}

class DemoListMultipleFrame extends JFrame
implements ListSelectionListener, ActionListener
{
   private JTextArea selectedList;
   private JList fileList;
   private JButton display;
   private JButton clear;
   private JButton exit;
   private String[] filenameArray;

   public DemoListMultipleFrame()
   {
      // get a list of filenames in the current directory

      File f = new File(".");
      filenameArray = f.list();

      // ----------------------------------
      // construct and configure components
      // ----------------------------------

      // create a list of filenames

      fileList = new JList(filenameArray);
      fileList.setSelectedIndex(0);

      // put the list in a scroll pane

      JScrollPane sp1 = new JScrollPane(fileList);
      sp1.setPreferredSize(new Dimension(400, 100));
      sp1.setBorder(BorderFactory.createLineBorder(Color.black));

      // create command buttons

      display = new JButton("Display");
      clear = new JButton("Clear");
      exit = new JButton("Exit");

      // create a text area to display the selected items

      selectedList = new JTextArea();
      selectedList.setMargin(new Insets(2, 2, 2, 2));

      // put the text area in a scroll pane

      JScrollPane sp2 = new JScrollPane(selectedList);
      sp2.setPreferredSize(new Dimension(400, 100));
      sp2.setBorder(BorderFactory.createLineBorder(Color.black));

      // -------------
      // add listeners
      // -------------

      fileList.addListSelectionListener(this);
      display.addActionListener(this);
      clear.addActionListener(this);
      exit.addActionListener(this);

      // -----------------
      // layout components
      // -----------------

      JPanel buttonPanel = new JPanel();
      buttonPanel.add(display);
      buttonPanel.add(clear);
      buttonPanel.add(exit);

      JPanel panel = new JPanel();
      panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
      panel.add(sp1);
      panel.add(Box.createRigidArea(new Dimension(0, 10)));
      panel.add(buttonPanel);
      panel.add(Box.createRigidArea(new Dimension(0, 10)));
      panel.add(sp2);
      panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

      // make panel this JFrame's content pane

      this.setContentPane(panel);
   }

   // --------------------------------------
   // implement ListSelectionListener method
   // --------------------------------------

   public void valueChanged(ListSelectionEvent lse)
   {
      int[] n = fileList.getSelectedIndices();
      System.out.println(n.length + " item(s) selected"); 
   }

   public void actionPerformed(ActionEvent ae)
   {
      JButton source = (JButton)ae.getSource();

      if (source == display)
      {
         int[] n = fileList.getSelectedIndices();
         String s = "";
         for (int i = 0; i < n.length; ++i)
            s += filenameArray[n[i]] + "\n";
         selectedList.setText(s);
      }
      else if (source == clear)
         selectedList.setText("");
      else if (source == exit)
         System.exit(0);
   }
}

