/Users/lyon/j4p/src/bookExamples/ch18Swing/JTableExample.java
|
1 package bookExamples.ch18Swing;
2
3 /*
4 * TableDemo.java is a 1.4 application that requires no other files.
5 */
6
7 import javax.swing.*;
8 import java.awt.*;
9
10 /**
11 * JTableExample uses a custom TableModel.
12 */
13 public class JTableExample {
14 private final JPanel jp = new JPanel(new GridLayout(1, 0));
15 private static final int VALUE = 20;
16 private static final int WIDTH = 500;
17 private static final int HEIGHT = 70;
18
19 /**
20 * A program to illustrate the use of the JTable
21 */
22 public JTableExample() {
23 String[] columnNames = {"First Name",
24 "Last Name",
25 "Sport",
26 "# of Years",
27 "Vegetarian"};
28 Object[][] data = {
29 {"Mary", "Campione",
30 "Snowboarding", new Integer(5), new Boolean(false)},
31 {"Alison", "Huml",
32 "Rowing", new Integer(3), new Boolean(true)},
33 {"Kathy", "Walrath",
34 "Knitting", new Integer(2), new Boolean(false)},
35 {"Sharon", "Zakhour",
36 "Speed reading", new Integer(VALUE), new Boolean(true)},
37 {"Philip", "Milne",
38 "Pool", new Integer(10), new Boolean(false)}
39 };
40 final TableData dm = new TableData(columnNames,data);
41 JTable table = new JTable(dm.getTableModel());
42 table.setPreferredScrollableViewportSize(new Dimension(WIDTH, HEIGHT));
43 jp.add(new JScrollPane(table));
44 }
45
46
47
48 private static void initGui() {
49 JFrame.setDefaultLookAndFeelDecorated(true);
50 JFrame frame = initWindow();
51 initContentPane(frame);
52 frame.pack();
53 frame.setVisible(true);
54 }
55
56 private static void initContentPane(JFrame frame) {
57 JTableExample newContentPane = new JTableExample();
58 //must paint every pixel in component bounds:
59 newContentPane.jp.setOpaque(true);
60 frame.setContentPane(newContentPane.jp);
61 }
62
63 private static JFrame initWindow() {
64 JFrame frame = new JFrame("JTableExample");
65 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
66 return frame;
67 }
68 /**
69 * Args are unused...just start and display the
70 * program.
71 * @param args
72 */
73 public static void main(String[] args) {
74 initGui();
75 }
76 }
77