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 |
* Another JTable example, showing how column attributes can be refined
|
|
37 |
* even when columns have been created automatically. Here we create some
|
|
38 |
* specialised renderers and editors as well as changing widths and colors
|
|
39 |
* for some of the columns in the SwingSet demo table.
|
|
40 |
*
|
|
41 |
* @author Philip Milne
|
|
42 |
*/
|
|
43 |
|
|
44 |
import javax.swing.*;
|
|
45 |
import javax.swing.table.*;
|
|
46 |
import javax.swing.border.*;
|
|
47 |
|
|
48 |
import java.awt.Dimension;
|
|
49 |
import java.awt.event.WindowAdapter;
|
|
50 |
import java.awt.event.WindowEvent;
|
|
51 |
import java.awt.Color;
|
|
52 |
|
|
53 |
public class TableExample4 {
|
|
54 |
|
|
55 |
public TableExample4() {
|
|
56 |
JFrame frame = new JFrame("Table");
|
|
57 |
frame.addWindowListener(new WindowAdapter() {
|
|
58 |
public void windowClosing(WindowEvent e) {System.exit(0);}});
|
|
59 |
|
|
60 |
// Take the dummy data from SwingSet.
|
|
61 |
final String[] names = {"First Name", "Last Name", "Favorite Color",
|
|
62 |
"Favorite Number", "Vegetarian"};
|
|
63 |
final Object[][] data = {
|
|
64 |
{"Mark", "Andrews", "Red", new Integer(2), Boolean.TRUE},
|
|
65 |
{"Tom", "Ball", "Blue", new Integer(99), Boolean.FALSE},
|
|
66 |
{"Alan", "Chung", "Green", new Integer(838), Boolean.FALSE},
|
|
67 |
{"Jeff", "Dinkins", "Turquois", new Integer(8), Boolean.TRUE},
|
|
68 |
{"Amy", "Fowler", "Yellow", new Integer(3), Boolean.FALSE},
|
|
69 |
{"Brian", "Gerhold", "Green", new Integer(0), Boolean.FALSE},
|
|
70 |
{"James", "Gosling", "Pink", new Integer(21), Boolean.FALSE},
|
|
71 |
{"David", "Karlton", "Red", new Integer(1), Boolean.FALSE},
|
|
72 |
{"Dave", "Kloba", "Yellow", new Integer(14), Boolean.FALSE},
|
|
73 |
{"Peter", "Korn", "Purple", new Integer(12), Boolean.FALSE},
|
|
74 |
{"Phil", "Milne", "Purple", new Integer(3), Boolean.FALSE},
|
|
75 |
{"Dave", "Moore", "Green", new Integer(88), Boolean.FALSE},
|
|
76 |
{"Hans", "Muller", "Maroon", new Integer(5), Boolean.FALSE},
|
|
77 |
{"Rick", "Levenson", "Blue", new Integer(2), Boolean.FALSE},
|
|
78 |
{"Tim", "Prinzing", "Blue", new Integer(22), Boolean.FALSE},
|
|
79 |
{"Chester", "Rose", "Black", new Integer(0), Boolean.FALSE},
|
|
80 |
{"Ray", "Ryan", "Gray", new Integer(77), Boolean.FALSE},
|
|
81 |
{"Georges", "Saab", "Red", new Integer(4), Boolean.FALSE},
|
|
82 |
{"Willie", "Walker", "Phthalo Blue", new Integer(4), Boolean.FALSE},
|
|
83 |
{"Kathy", "Walrath", "Blue", new Integer(8), Boolean.FALSE},
|
|
84 |
{"Arnaud", "Weber", "Green", new Integer(44), Boolean.FALSE}
|
|
85 |
};
|
|
86 |
|
|
87 |
// Create a model of the data.
|
|
88 |
TableModel dataModel = new AbstractTableModel() {
|
|
89 |
// These methods always need to be implemented.
|
|
90 |
public int getColumnCount() { return names.length; }
|
|
91 |
public int getRowCount() { return data.length;}
|
|
92 |
public Object getValueAt(int row, int col) {return data[row][col];}
|
|
93 |
|
|
94 |
// The default implementations of these methods in
|
|
95 |
// AbstractTableModel would work, but we can refine them.
|
|
96 |
public String getColumnName(int column) {return names[column];}
|
|
97 |
public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
|
|
98 |
public boolean isCellEditable(int row, int col) {return true;}
|
|
99 |
public void setValueAt(Object aValue, int row, int column) {
|
|
100 |
System.out.println("Setting value to: " + aValue);
|
|
101 |
data[row][column] = aValue;
|
|
102 |
}
|
|
103 |
};
|
|
104 |
|
|
105 |
// Create the table
|
|
106 |
JTable tableView = new JTable(dataModel);
|
|
107 |
// Turn off auto-resizing so that we can set column sizes programmatically.
|
|
108 |
// In this mode, all columns will get their preferred widths, as set blow.
|
|
109 |
tableView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
|
|
110 |
|
|
111 |
// Create a combo box to show that you can use one in a table.
|
|
112 |
JComboBox comboBox = new JComboBox();
|
|
113 |
comboBox.addItem("Red");
|
|
114 |
comboBox.addItem("Orange");
|
|
115 |
comboBox.addItem("Yellow");
|
|
116 |
comboBox.addItem("Green");
|
|
117 |
comboBox.addItem("Blue");
|
|
118 |
comboBox.addItem("Indigo");
|
|
119 |
comboBox.addItem("Violet");
|
|
120 |
|
|
121 |
TableColumn colorColumn = tableView.getColumn("Favorite Color");
|
|
122 |
// Use the combo box as the editor in the "Favorite Color" column.
|
|
123 |
colorColumn.setCellEditor(new DefaultCellEditor(comboBox));
|
|
124 |
|
|
125 |
// Set a pink background and tooltip for the Color column renderer.
|
|
126 |
DefaultTableCellRenderer colorColumnRenderer = new DefaultTableCellRenderer();
|
|
127 |
colorColumnRenderer.setBackground(Color.pink);
|
|
128 |
colorColumnRenderer.setToolTipText("Click for combo box");
|
|
129 |
colorColumn.setCellRenderer(colorColumnRenderer);
|
|
130 |
|
|
131 |
// Set a tooltip for the header of the colors column.
|
|
132 |
TableCellRenderer headerRenderer = colorColumn.getHeaderRenderer();
|
|
133 |
if (headerRenderer instanceof DefaultTableCellRenderer)
|
|
134 |
((DefaultTableCellRenderer)headerRenderer).setToolTipText("Hi Mom!");
|
|
135 |
|
|
136 |
// Set the width of the "Vegetarian" column.
|
|
137 |
TableColumn vegetarianColumn = tableView.getColumn("Vegetarian");
|
|
138 |
vegetarianColumn.setPreferredWidth(100);
|
|
139 |
|
|
140 |
// Show the values in the "Favorite Number" column in different colors.
|
|
141 |
TableColumn numbersColumn = tableView.getColumn("Favorite Number");
|
|
142 |
DefaultTableCellRenderer numberColumnRenderer = new DefaultTableCellRenderer() {
|
|
143 |
public void setValue(Object value) {
|
|
144 |
int cellValue = (value instanceof Number) ? ((Number)value).intValue() : 0;
|
|
145 |
setForeground((cellValue > 30) ? Color.black : Color.red);
|
|
146 |
setText((value == null) ? "" : value.toString());
|
|
147 |
}
|
|
148 |
};
|
|
149 |
numberColumnRenderer.setHorizontalAlignment(JLabel.RIGHT);
|
|
150 |
numbersColumn.setCellRenderer(numberColumnRenderer);
|
|
151 |
numbersColumn.setPreferredWidth(110);
|
|
152 |
|
|
153 |
// Finish setting up the table.
|
|
154 |
JScrollPane scrollpane = new JScrollPane(tableView);
|
|
155 |
scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));
|
|
156 |
scrollpane.setPreferredSize(new Dimension(430, 200));
|
|
157 |
frame.getContentPane().add(scrollpane);
|
|
158 |
frame.pack();
|
|
159 |
frame.setVisible(true);
|
|
160 |
}
|
|
161 |
|
|
162 |
public static void main(String[] args) {
|
|
163 |
new TableExample4();
|
|
164 |
}
|
|
165 |
}
|