KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TableExample3


1 /*
2  * @(#)TableExample3.java 1.17 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)TableExample3.java 1.17 05/11/17
39  */

40
41 /**
42  * An example showing the JTable with a dataModel that is not derived
43  * from a database. We add the optional TableSorter object to give the
44  * JTable the ability to sort.
45  *
46  * @version 1.3 10/14/97
47  * @author Philip Milne
48  */

49
50 import javax.swing.*;
51 import javax.swing.table.*;
52
53 import java.awt.event.WindowAdapter JavaDoc;
54 import java.awt.event.WindowEvent JavaDoc;
55 import java.awt.Dimension JavaDoc;
56
57 public class TableExample3 {
58
59     public TableExample3() {
60         JFrame frame = new JFrame("Table");
61         frame.addWindowListener(new WindowAdapter JavaDoc() {
62             public void windowClosing(WindowEvent JavaDoc e) {System.exit(0);}});
63
64         // Take the dummy data from SwingSet.
65
final String JavaDoc[] names = {"First Name", "Last Name", "Favorite Color",
66                                 "Favorite Number", "Vegetarian"};
67         final Object JavaDoc[][] data = {
68         {"Mark", "Andrews", "Red", new Integer JavaDoc(2), Boolean.TRUE},
69         {"Tom", "Ball", "Blue", new Integer JavaDoc(99), Boolean.FALSE},
70         {"Alan", "Chung", "Green", new Integer JavaDoc(838), Boolean.FALSE},
71         {"Jeff", "Dinkins", "Turquois", new Integer JavaDoc(8), Boolean.TRUE},
72         {"Amy", "Fowler", "Yellow", new Integer JavaDoc(3), Boolean.FALSE},
73         {"Brian", "Gerhold", "Green", new Integer JavaDoc(0), Boolean.FALSE},
74         {"James", "Gosling", "Pink", new Integer JavaDoc(21), Boolean.FALSE},
75         {"David", "Karlton", "Red", new Integer JavaDoc(1), Boolean.FALSE},
76         {"Dave", "Kloba", "Yellow", new Integer JavaDoc(14), Boolean.FALSE},
77         {"Peter", "Korn", "Purple", new Integer JavaDoc(12), Boolean.FALSE},
78         {"Phil", "Milne", "Purple", new Integer JavaDoc(3), Boolean.FALSE},
79         {"Dave", "Moore", "Green", new Integer JavaDoc(88), Boolean.FALSE},
80         {"Hans", "Muller", "Maroon", new Integer JavaDoc(5), Boolean.FALSE},
81         {"Rick", "Levenson", "Blue", new Integer JavaDoc(2), Boolean.FALSE},
82         {"Tim", "Prinzing", "Blue", new Integer JavaDoc(22), Boolean.FALSE},
83         {"Chester", "Rose", "Black", new Integer JavaDoc(0), Boolean.FALSE},
84         {"Ray", "Ryan", "Gray", new Integer JavaDoc(77), Boolean.FALSE},
85         {"Georges", "Saab", "Red", new Integer JavaDoc(4), Boolean.FALSE},
86         {"Willie", "Walker", "Phthalo Blue", new Integer JavaDoc(4), Boolean.FALSE},
87         {"Kathy", "Walrath", "Blue", new Integer JavaDoc(8), Boolean.FALSE},
88         {"Arnaud", "Weber", "Green", new Integer JavaDoc(44), Boolean.FALSE}
89         };
90
91         // Create a model of the data.
92
TableModel dataModel = new AbstractTableModel() {
93             // These methods always need to be implemented.
94
public int getColumnCount() { return names.length; }
95             public int getRowCount() { return data.length;}
96             public Object JavaDoc getValueAt(int row, int col) {return data[row][col];}
97
98             // The default implementations of these methods in
99
// AbstractTableModel would work, but we can refine them.
100
public String JavaDoc getColumnName(int column) {return names[column];}
101             public Class JavaDoc getColumnClass(int col) {return getValueAt(0,col).getClass();}
102             public boolean isCellEditable(int row, int col) {return (col==4);}
103             public void setValueAt(Object JavaDoc aValue, int row, int column) {
104                 data[row][column] = aValue;
105             }
106          };
107
108         // Instead of making the table display the data as it would normally with:
109
// JTable tableView = new JTable(dataModel);
110
// Add a sorter, by using the following three lines instead of the one above.
111
TableSorter sorter = new TableSorter(dataModel);
112         JTable tableView = new JTable(sorter);
113         sorter.addMouseListenerToHeaderInTable(tableView);
114
115         JScrollPane scrollpane = new JScrollPane(tableView);
116
117         scrollpane.setPreferredSize(new Dimension JavaDoc(700, 300));
118         frame.getContentPane().add(scrollpane);
119         frame.pack();
120         frame.setVisible(true);
121     }
122
123     public static void main(String JavaDoc[] args) {
124         new TableExample3();
125     }
126 }
127
Popular Tags