/home/wpollock1/public_html/AJava/JTableDemo.java

// Simple JTable Demo, which uses the DefaultTableModel.
// Written 9/2014 by Wayne Pollock, Tampa Florida USA.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JTableDemo {
   private static JTable simpleTbl;

   public static void main ( String [] args ) {
      // Build the "view":
      JFrame simple = new JFrame( "JTable Demo - Simple Model" );
      simple.setLayout( new BorderLayout() );
      simple.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      simple.setVisible( true );  // Must make visible to get FontMetrics!

      // Set the initial view from the (initial) model:
      JTable simpleTbl = buildSimpleModel();

      // Set the table (frame) size, so all columns have same width as
      // for "Wednesday", the widest column (the "10" and "4" determined by
      // trial and error):
      FontMetrics fm = simple.getGraphics().getFontMetrics();
      int colWidth = fm.stringWidth( " Wednesday " ) + 10;
      int colHeight = fm.getHeight() + 4;
      simple.setPreferredSize( new Dimension(colWidth * 8, colHeight * 17));

      simple.add( new JScrollPane( simpleTbl ), BorderLayout.CENTER );
      JPanel p = new JPanel();
      p.add( new JLabel("Click on a cell to change its value.") );
      simple.add( p, BorderLayout.SOUTH );
      simple.pack();
      simple.setLocationRelativeTo( null );  // Center on screen.

      // The "controller": define the behavior as people click:
      simpleTbl.setRowSelectionAllowed( false );  // only cell selection

      // Edit a cell by clicking on it:
      simpleTbl.addMouseListener( new MouseAdapter() {
    	  @Override public void mousePressed( MouseEvent me ) {
		     JTable target = (JTable) me.getSource();
		      int row = target.getSelectedRow();
		      int col = target.getSelectedColumn();
		      String course = JOptionPane.showInputDialog(null, "Enter Course Number:");
		      if ( course != null )
		    	  target.getModel().setValueAt(course, row, col);
    	  }
      });
   }

   static JTable buildSimpleModel ( ) {

      // The model: A JTable is just a view/controller; the data is in
      // the model.  Here we use the simple, built-in model:

      String[] colHeads = {
         "Time", "Monday", "Tuesday", "Wednesday",
         "Thursday", "Friday", "Saturday", "  on-line  "
      };

      String[][] initialData = {
         { "  8 AM  ", "", "", "", "", "", "", ""},
         { "  9 AM  ", "", "", "", "", "", "", ""},
         { " 10 AM  ", "", "", "", "", "", "", ""},
         { " 11 AM  ", "", "", "", "", "", "", ""},
         { "  Noon  ", "", "", "", "", "", "", ""},
         { "  1 PM  ", "", "", "", "", "", "", ""},
         { "  2 PM  ", "", "", "", "", "", "", ""},
         { "  3 PM  ", "", "", "", "", "", "", ""},
         { "  4 PM  ", "", "", "", "", "", "", ""},
         { "  5 PM  ", "", "", "", "", "", "", ""},
         { "  6 PM  ", "", "", "", "", "", "", ""},
         { "  7 PM  ", "", "", "", "", "", "", ""},
         { "  8 PM  ", "", "", "", "", "", "", ""},
         { "  9 PM  ", "", "", "", "", "", "", ""},
         { " 10 PM  ", "", "", "", "", "", "", ""}
      };

      simpleTbl = new JTable( initialData, colHeads );
      return simpleTbl;
   }
}