javax.swing.JTable


Here's a simple example of creating a JTable instance.  Notice the use of Vector instances in the JTable's constructor.

JTable Image

JTableExample.java source

01: import javax.swing.*;
02: import java.util.*;
03: import java.awt.*;
04: import java.awt.event.*;
05: public class JTableExample extends JFrame
06: {
07:    public JTableExample(String title)
08:    {
09:       super(title);
10: 
11:       String[] names = {"zero", "one", "two", "three"};
12:       Vector colNames = new Vector();
13:       colNames.addAll(Arrays.asList(names));
14: 
15:       Vector rows = new Vector();
16:       for (int i = 0; i < 5; i++)
17:       {
18:          Vector aRow = new Vector();
19:          for (int j = 0; j < 5; j++)
20:           aRow.add(new Integer(i*j));
21:          rows.add(aRow);
22:       }
23: 
24:       JTable table = new JTable(rows, colNames);
25:       table.setPreferredScrollableViewportSize(
26:          new Dimension(200,100));
27: 
28:       JScrollPane scrollPane = new JScrollPane(table);
29:       getContentPane().add(scrollPane, BorderLayout.CENTER);
30: 
31:       addWindowListener(
32:          new WindowAdapter()
33:          {
34:             public void windowClosing(WindowEvent e)
35:             {
36:                System.exit(0);
37:             }
38:          });
39:    }
40: 
41:    public static void main( String args[] )
42:    {
43:       JTableExample aTable = new JTableExample("The Table");
44:       aTable.pack();
45:       aTable.setVisible(true);
46:    }
47: }