Home Segments Top Top Previous Next

980: Mainline

The table shown in Segment 979 is produced with the following program. Note that the table must be embedded in a scroll pane; otherwise, the column labels will not appear:

import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableDemonstration {
 public static void main (String argv []) {
  // Prepare table contents
  Vector columns = new Vector();
  columns.add("Column 0");
  columns.add("Column 1");
  Vector row0 = new Vector();
  row0.add("Cell 0, 0");
  row0.add("Cell 0, 1");
  Vector row1 = new Vector();
  row1.add("Cell 1, 0");
  row1.add("Cell 1, 1");
  Vector rows = new Vector();
  rows.add(row0);
  rows.add(row1);
  // Make the table
  JTable table = new JTable(rows, columns);                     
  // Show the table
  JFrame frame = new JFrame("Table Demonstration");
  frame.getContentPane().add("Center", new JScrollPane(table)); 
  frame.setSize(250, 80); 
  frame.addWindowListener(new ApplicationClosingWindowListener()); 
  frame.show(); 
 } 
}