JTableDemo.java

Download JTableDemo.java

 1: // Simple JTable Demo, which uses the DefaultTableModel.
 2: // Written 9/2014 by Wayne Pollock, Tampa Florida USA.
 3: 
 4: import java.awt.*;
 5: import java.awt.event.*;
 6: import javax.swing.*;
 7: 
 8: public class JTableDemo {
 9:    private static JTable simpleTbl;
10: 
11:    public static void main ( String [] args ) {
12:       // Build the "view":
13:       JFrame simple = new JFrame( "JTable Demo - Simple Model" );
14:       simple.setLayout( new BorderLayout() );
15:       simple.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
16:       simple.setVisible( true );  // Must make visible to get FontMetrics!
17: 
18:       // Set the initial view from the (initial) model:
19:       JTable simpleTbl = buildSimpleModel();
20: 
21:       // Set the table (frame) size, so all columns have same width as
22:       // for "Wednesday", the widest column (the "10" and "4" determined by
23:       // trial and error):
24:       FontMetrics fm = simple.getGraphics().getFontMetrics();
25:       int colWidth = fm.stringWidth( " Wednesday " ) + 10;
26:       int colHeight = fm.getHeight() + 4;
27:       simple.setPreferredSize( new Dimension(colWidth * 8, colHeight * 17));
28: 
29:       simple.add( new JScrollPane( simpleTbl ), BorderLayout.CENTER );
30:       JPanel p = new JPanel();
31:       p.add( new JLabel("Click on a cell to change its value.") );
32:       simple.add( p, BorderLayout.SOUTH );
33:       simple.pack();
34:       simple.setLocationRelativeTo( null );  // Center on screen.
35: 
36:       // The "controller": define the behavior as people click:
37:       simpleTbl.setRowSelectionAllowed( false );  // only cell selection
38: 
39:       // Edit a cell by clicking on it:
40:       simpleTbl.addMouseListener( new MouseAdapter() {
41:     	  @Override public void mousePressed( MouseEvent me ) {
42: 		     JTable target = (JTable) me.getSource();
43: 		      int row = target.getSelectedRow();
44: 		      int col = target.getSelectedColumn();
45: 		      String course = JOptionPane.showInputDialog(null, "Enter Course Number:");
46: 		      if ( course != null )
47: 		    	  target.getModel().setValueAt(course, row, col);
48:     	  }
49:       });
50:    }
51: 
52:    static JTable buildSimpleModel ( ) {
53: 
54:       // The model: A JTable is just a view/controller; the data is in
55:       // the model.  Here we use the simple, built-in model:
56: 
57:       String[] colHeads = {
58:          "Time", "Monday", "Tuesday", "Wednesday",
59:          "Thursday", "Friday", "Saturday", "  on-line  "
60:       };
61: 
62:       String[][] initialData = {
63:          { "  8 AM  ", "", "", "", "", "", "", ""},
64:          { "  9 AM  ", "", "", "", "", "", "", ""},
65:          { " 10 AM  ", "", "", "", "", "", "", ""},
66:          { " 11 AM  ", "", "", "", "", "", "", ""},
67:          { "  Noon  ", "", "", "", "", "", "", ""},
68:          { "  1 PM  ", "", "", "", "", "", "", ""},
69:          { "  2 PM  ", "", "", "", "", "", "", ""},
70:          { "  3 PM  ", "", "", "", "", "", "", ""},
71:          { "  4 PM  ", "", "", "", "", "", "", ""},
72:          { "  5 PM  ", "", "", "", "", "", "", ""},
73:          { "  6 PM  ", "", "", "", "", "", "", ""},
74:          { "  7 PM  ", "", "", "", "", "", "", ""},
75:          { "  8 PM  ", "", "", "", "", "", "", ""},
76:          { "  9 PM  ", "", "", "", "", "", "", ""},
77:          { " 10 PM  ", "", "", "", "", "", "", ""}
78:       };
79: 
80:       simpleTbl = new JTable( initialData, colHeads );
81:       return simpleTbl;
82:    }
83: }