2
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
8
* - Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
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.
15
* - Neither the name of Oracle 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.
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.
33
* This source code is provided to illustrate the usage of a given feature
34
* or technique and has been deliberately simplified. Additional steps
35
* required for a production-quality application, such as security checks,
36
* input validation and proper error handling, might not be present in
43
import javax.swing.table.*;
45
import java.awt.event.WindowAdapter;
46
import java.awt.event.WindowEvent;
47
import java.awt.Dimension;
48
import java.util.logging.Level;
49
import java.util.logging.Logger;
50
import javax.swing.UIManager.LookAndFeelInfo;
54
* An example showing the JTable with a dataModel that is not derived
55
* from a database. We add the optional TableSorter object to give the
56
* JTable the ability to sort.
58
* @author Philip Milne
60
public class TableExample3 {
62
public TableExample3() {
63
JFrame frame = new JFrame("Table");
64
frame.addWindowListener(new WindowAdapter() {
67
public void windowClosing(WindowEvent e) {
72
// Take the dummy data from SwingSet.
73
final String[] names = { "First Name", "Last Name", "Favorite Color",
74
"Favorite Number", "Vegetarian" };
75
final Object[][] data = {
76
{ "Mark", "Andrews", "Red", Integer.valueOf(2), Boolean.TRUE },
77
{ "Tom", "Ball", "Blue", Integer.valueOf(99), Boolean.FALSE },
78
{ "Alan", "Chung", "Green", Integer.valueOf(838), Boolean.FALSE },
79
{ "Jeff", "Dinkins", "Turquois", Integer.valueOf(8), Boolean.TRUE },
80
{ "Amy", "Fowler", "Yellow", Integer.valueOf(3), Boolean.FALSE },
81
{ "Brian", "Gerhold", "Green", Integer.valueOf(0), Boolean.FALSE },
82
{ "James", "Gosling", "Pink", Integer.valueOf(21), Boolean.FALSE },
83
{ "David", "Karlton", "Red", Integer.valueOf(1), Boolean.FALSE },
84
{ "Dave", "Kloba", "Yellow", Integer.valueOf(14), Boolean.FALSE },
85
{ "Peter", "Korn", "Purple", Integer.valueOf(12), Boolean.FALSE },
86
{ "Phil", "Milne", "Purple", Integer.valueOf(3), Boolean.FALSE },
87
{ "Dave", "Moore", "Green", Integer.valueOf(88), Boolean.FALSE },
88
{ "Hans", "Muller", "Maroon", Integer.valueOf(5), Boolean.FALSE },
89
{ "Rick", "Levenson", "Blue", Integer.valueOf(2), Boolean.FALSE },
90
{ "Tim", "Prinzing", "Blue", Integer.valueOf(22), Boolean.FALSE },
91
{ "Chester", "Rose", "Black", Integer.valueOf(0), Boolean.FALSE },
92
{ "Ray", "Ryan", "Gray", Integer.valueOf(77), Boolean.FALSE },
93
{ "Georges", "Saab", "Red", Integer.valueOf(4), Boolean.FALSE },
94
{ "Willie", "Walker", "Phthalo Blue", Integer.valueOf(4), Boolean.FALSE },
95
{ "Kathy", "Walrath", "Blue", Integer.valueOf(8), Boolean.FALSE },
96
{ "Arnaud", "Weber", "Green", Integer.valueOf(44), Boolean.FALSE }
99
// Create a model of the data.
100
@SuppressWarnings("serial")
101
TableModel dataModel = new AbstractTableModel() {
102
// These methods always need to be implemented.
104
public int getColumnCount() {
108
public int getRowCount() {
112
public Object getValueAt(int row, int col) {
113
return data[row][col];
116
// The default implementations of these methods in
117
// AbstractTableModel would work, but we can refine them.
119
public String getColumnName(int column) {
120
return names[column];
124
public Class<?> getColumnClass(int col) {
125
return getValueAt(0, col).getClass();
129
public boolean isCellEditable(int row, int col) {
134
public void setValueAt(Object aValue, int row, int column) {
135
data[row][column] = aValue;
139
// Instead of making the table display the data as it would normally
141
// JTable tableView = new JTable(dataModel);
142
// Add a sorter, by using the following three lines instead of the one
144
TableSorter sorter = new TableSorter(dataModel);
145
JTable tableView = new JTable(sorter);
146
sorter.addMouseListenerToHeaderInTable(tableView);
148
JScrollPane scrollpane = new JScrollPane(tableView);
150
scrollpane.setPreferredSize(new Dimension(700, 300));
151
frame.getContentPane().add(scrollpane);
153
frame.setVisible(true);
156
public static void main(String[] args) {
157
// Trying to set Nimbus look and feel
159
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
160
if ("Nimbus".equals(info.getName())) {
161
UIManager.setLookAndFeel(info.getClassName());
165
} catch (Exception ex) {
166
Logger.getLogger(TableExample3.class.getName()).log(Level.SEVERE,
167
"Failed to apply Nimbus look and feel", ex);