import java.awt.*;
import javax.swing.*;

/**
 * A demonstration of drag and drop with text components that
 * support both "out of the box".
 * 
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class OutOfTheBoxDemo implements Runnable
{
  /**
   * Create and show the GUI.
   * 
   * @param args The command line arguments (which are ignored)
   * @throws Exception if something goes wrong
   */
  public static void main(String[] args) throws Exception
  {
    SwingUtilities.invokeAndWait(new OutOfTheBoxDemo());
  }

  /**
   * The code to execute in the event dispatch thread.
   */
  public void run()
  {
    JFrame f = new JFrame("DnD Out of the Box");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(640, 480);

    JPanel cp = (JPanel)f.getContentPane();
    cp.setLayout(new BorderLayout());
    cp.add(new JLabel("Drag from the JList; Drop into the JTextArea", JLabel.CENTER),
        BorderLayout.NORTH);
    
    // The source
    String[] data = {"CS149", "CS159", "CS240", "CS261", "CS345", "CS361"};
    JList<String> list = new JList<String>(data);
    list.setBorder(BorderFactory.createTitledBorder("Offered"));
    list.setFixedCellWidth(100);
    list.setDragEnabled(true); // Drag support must be enabled manually
    cp.add(list,  BorderLayout.WEST);
    
    // The target
    JTextArea textArea = new JTextArea(); // Drop support is enabled by default
    textArea.setBorder(BorderFactory.createTitledBorder("Completed"));
    cp.add(textArea, BorderLayout.CENTER);
    
    f.setVisible(true);
  }
}
