import java.awt.*;
import javax.swing.*;
import javax.swing.table.TableColumn;

/**
 * A demonstration of a custom TableCellEditor.
 * 
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class CourseCellEditorDemo implements Runnable
{
  /**
   * Create the GUI.
   * 
   * @param args  The command line arguments (which are ignored)
   * @throws Exception if anything goes wrong
   */
  public static void main(String[] args) throws Exception
  {
    SwingUtilities.invokeAndWait(new CourseCellEditorDemo());
  }
 
  /**
   * The code to run in the event dispatch thread.
   */
  public void run()
  {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(640, 480);
    
    JPanel contentPane = (JPanel)f.getContentPane();
    contentPane.setLayout(new BorderLayout());

    // Create the model for the JTable
    String[] columnNames = {"Courses"};
    Object[][] data = {{new Course("CS", "101")}};
    
    GenericTableModel model = new GenericTableModel(data, columnNames);
    for (int i=0; i<10; i++) model.addRow(data[0]);

    JTable table = new JTable(model);

    for (int i=0; i<columnNames.length; i++) 
    {
      TableColumn column = table.getColumn(columnNames[i]);
      column.setPreferredWidth(200);
    }
    table.setRowHeight(45);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    // Use the default TableCellRenderer (which calls the Object's toString())
    
    // Use a custom TableCellEditor
    table.setDefaultEditor(Course.class, new CourseCellEditor());
    
    contentPane.add(new JScrollPane(table),  BorderLayout.CENTER);
    
    f.setVisible(true);
  }
}
