2
|
1 |
/*
|
|
2 |
* Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
*
|
|
4 |
* Redistribution and use in source and binary forms, with or without
|
|
5 |
* modification, are permitted provided that the following conditions
|
|
6 |
* are met:
|
|
7 |
*
|
|
8 |
* - Redistributions of source code must retain the above copyright
|
|
9 |
* notice, this list of conditions and the following disclaimer.
|
|
10 |
*
|
|
11 |
* - Redistributions in binary form must reproduce the above copyright
|
|
12 |
* notice, this list of conditions and the following disclaimer in the
|
|
13 |
* documentation and/or other materials provided with the distribution.
|
|
14 |
*
|
|
15 |
* - Neither the name of Sun Microsystems nor the names of its
|
|
16 |
* contributors may be used to endorse or promote products derived
|
|
17 |
* from this software without specific prior written permission.
|
|
18 |
*
|
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
30 |
*/
|
|
31 |
|
|
32 |
/*
|
|
33 |
*/
|
|
34 |
|
|
35 |
/**
|
|
36 |
* An example showing the JTable with a dataModel that is not derived
|
|
37 |
* from a database. We add the optional TableSorter object to give the
|
|
38 |
* JTable the ability to sort.
|
|
39 |
*
|
|
40 |
* @author Philip Milne
|
|
41 |
*/
|
|
42 |
|
|
43 |
import javax.swing.*;
|
|
44 |
import javax.swing.table.*;
|
|
45 |
|
|
46 |
import java.awt.event.WindowAdapter;
|
|
47 |
import java.awt.event.WindowEvent;
|
|
48 |
import java.awt.Dimension;
|
|
49 |
|
|
50 |
public class TableExample3 {
|
|
51 |
|
|
52 |
public TableExample3() {
|
|
53 |
JFrame frame = new JFrame("Table");
|
|
54 |
frame.addWindowListener(new WindowAdapter() {
|
|
55 |
public void windowClosing(WindowEvent e) {System.exit(0);}});
|
|
56 |
|
|
57 |
// Take the dummy data from SwingSet.
|
|
58 |
final String[] names = {"First Name", "Last Name", "Favorite Color",
|
|
59 |
"Favorite Number", "Vegetarian"};
|
|
60 |
final Object[][] data = {
|
|
61 |
{"Mark", "Andrews", "Red", new Integer(2), Boolean.TRUE},
|
|
62 |
{"Tom", "Ball", "Blue", new Integer(99), Boolean.FALSE},
|
|
63 |
{"Alan", "Chung", "Green", new Integer(838), Boolean.FALSE},
|
|
64 |
{"Jeff", "Dinkins", "Turquois", new Integer(8), Boolean.TRUE},
|
|
65 |
{"Amy", "Fowler", "Yellow", new Integer(3), Boolean.FALSE},
|
|
66 |
{"Brian", "Gerhold", "Green", new Integer(0), Boolean.FALSE},
|
|
67 |
{"James", "Gosling", "Pink", new Integer(21), Boolean.FALSE},
|
|
68 |
{"David", "Karlton", "Red", new Integer(1), Boolean.FALSE},
|
|
69 |
{"Dave", "Kloba", "Yellow", new Integer(14), Boolean.FALSE},
|
|
70 |
{"Peter", "Korn", "Purple", new Integer(12), Boolean.FALSE},
|
|
71 |
{"Phil", "Milne", "Purple", new Integer(3), Boolean.FALSE},
|
|
72 |
{"Dave", "Moore", "Green", new Integer(88), Boolean.FALSE},
|
|
73 |
{"Hans", "Muller", "Maroon", new Integer(5), Boolean.FALSE},
|
|
74 |
{"Rick", "Levenson", "Blue", new Integer(2), Boolean.FALSE},
|
|
75 |
{"Tim", "Prinzing", "Blue", new Integer(22), Boolean.FALSE},
|
|
76 |
{"Chester", "Rose", "Black", new Integer(0), Boolean.FALSE},
|
|
77 |
{"Ray", "Ryan", "Gray", new Integer(77), Boolean.FALSE},
|
|
78 |
{"Georges", "Saab", "Red", new Integer(4), Boolean.FALSE},
|
|
79 |
{"Willie", "Walker", "Phthalo Blue", new Integer(4), Boolean.FALSE},
|
|
80 |
{"Kathy", "Walrath", "Blue", new Integer(8), Boolean.FALSE},
|
|
81 |
{"Arnaud", "Weber", "Green", new Integer(44), Boolean.FALSE}
|
|
82 |
};
|
|
83 |
|
|
84 |
// Create a model of the data.
|
|
85 |
TableModel dataModel = new AbstractTableModel() {
|
|
86 |
// These methods always need to be implemented.
|
|
87 |
public int getColumnCount() { return names.length; }
|
|
88 |
public int getRowCount() { return data.length;}
|
|
89 |
public Object getValueAt(int row, int col) {return data[row][col];}
|
|
90 |
|
|
91 |
// The default implementations of these methods in
|
|
92 |
// AbstractTableModel would work, but we can refine them.
|
|
93 |
public String getColumnName(int column) {return names[column];}
|
|
94 |
public Class getColumnClass(int col) {return getValueAt(0,col).getClass();}
|
|
95 |
public boolean isCellEditable(int row, int col) {return (col==4);}
|
|
96 |
public void setValueAt(Object aValue, int row, int column) {
|
|
97 |
data[row][column] = aValue;
|
|
98 |
}
|
|
99 |
};
|
|
100 |
|
|
101 |
// Instead of making the table display the data as it would normally with:
|
|
102 |
// JTable tableView = new JTable(dataModel);
|
|
103 |
// Add a sorter, by using the following three lines instead of the one above.
|
|
104 |
TableSorter sorter = new TableSorter(dataModel);
|
|
105 |
JTable tableView = new JTable(sorter);
|
|
106 |
sorter.addMouseListenerToHeaderInTable(tableView);
|
|
107 |
|
|
108 |
JScrollPane scrollpane = new JScrollPane(tableView);
|
|
109 |
|
|
110 |
scrollpane.setPreferredSize(new Dimension(700, 300));
|
|
111 |
frame.getContentPane().add(scrollpane);
|
|
112 |
frame.pack();
|
|
113 |
frame.setVisible(true);
|
|
114 |
}
|
|
115 |
|
|
116 |
public static void main(String[] args) {
|
|
117 |
new TableExample3();
|
|
118 |
}
|
|
119 |
}
|