43
import javax.swing.table.*;
44
import javax.swing.border.*;
45
import java.awt.Dimension;
46
import java.awt.event.WindowAdapter;
47
import java.awt.event.WindowEvent;
49
import java.util.logging.Level;
50
import java.util.logging.Logger;
51
import javax.swing.UIManager.LookAndFeelInfo;
62
public class TableExample4 {
64
public TableExample4() {
65
JFrame frame = new JFrame("Table");
66
frame.addWindowListener(new WindowAdapter() {
69
public void windowClosing(WindowEvent e) {
75
final String[] names = { "First Name", "Last Name", "Favorite Color",
76
"Favorite Number", "Vegetarian" };
77
final Object[][] data = {
78
{ "Mark", "Andrews", "Red", Integer.valueOf(2), Boolean.TRUE },
79
{ "Tom", "Ball", "Blue", Integer.valueOf(99), Boolean.FALSE },
80
{ "Alan", "Chung", "Green", Integer.valueOf(838), Boolean.FALSE },
81
{ "Jeff", "Dinkins", "Turquois", Integer.valueOf(8), Boolean.TRUE },
82
{ "Amy", "Fowler", "Yellow", Integer.valueOf(3), Boolean.FALSE },
83
{ "Brian", "Gerhold", "Green", Integer.valueOf(0), Boolean.FALSE },
84
{ "James", "Gosling", "Pink", Integer.valueOf(21), Boolean.FALSE },
85
{ "David", "Karlton", "Red", Integer.valueOf(1), Boolean.FALSE },
86
{ "Dave", "Kloba", "Yellow", Integer.valueOf(14), Boolean.FALSE },
87
{ "Peter", "Korn", "Purple", Integer.valueOf(12), Boolean.FALSE },
88
{ "Phil", "Milne", "Purple", Integer.valueOf(3), Boolean.FALSE },
89
{ "Dave", "Moore", "Green", Integer.valueOf(88), Boolean.FALSE },
90
{ "Hans", "Muller", "Maroon", Integer.valueOf(5), Boolean.FALSE },
91
{ "Rick", "Levenson", "Blue", Integer.valueOf(2), Boolean.FALSE },
92
{ "Tim", "Prinzing", "Blue", Integer.valueOf(22), Boolean.FALSE },
93
{ "Chester", "Rose", "Black", Integer.valueOf(0), Boolean.FALSE },
94
{ "Ray", "Ryan", "Gray", Integer.valueOf(77), Boolean.FALSE },
95
{ "Georges", "Saab", "Red", Integer.valueOf(4), Boolean.FALSE },
96
{ "Willie", "Walker", "Phthalo Blue", Integer.valueOf(4), Boolean.FALSE },
97
{ "Kathy", "Walrath", "Blue", Integer.valueOf(8), Boolean.FALSE },
98
{ "Arnaud", "Weber", "Green", Integer.valueOf(44), Boolean.FALSE }
102
@SuppressWarnings("serial")
103
TableModel dataModel = new AbstractTableModel() {
106
public int getColumnCount() {
110
public int getRowCount() {
114
public Object getValueAt(int row, int col) {
115
return data[row][col];
121
public String getColumnName(int column) {
122
return names[column];
126
public Class<?> getColumnClass(int c) {
127
return getValueAt(0, c).getClass();
131
public boolean isCellEditable(int row, int col) {
136
public void setValueAt(Object aValue, int row, int column) {
137
System.out.println("Setting value to: " + aValue);
138
data[row][column] = aValue;
143
JTable tableView = new JTable(dataModel);
147
tableView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
150
JComboBox<String> comboBox = new JComboBox<>();
151
comboBox.addItem("Red");
152
comboBox.addItem("Orange");
153
comboBox.addItem("Yellow");
154
comboBox.addItem("Green");
155
comboBox.addItem("Blue");
156
comboBox.addItem("Indigo");
157
comboBox.addItem("Violet");
159
TableColumn colorColumn = tableView.getColumn("Favorite Color");
161
colorColumn.setCellEditor(new DefaultCellEditor(comboBox));
164
DefaultTableCellRenderer colorColumnRenderer =
165
new DefaultTableCellRenderer();
166
colorColumnRenderer.setBackground(Color.pink);
167
colorColumnRenderer.setToolTipText("Click for combo box");
168
colorColumn.setCellRenderer(colorColumnRenderer);
171
TableCellRenderer headerRenderer = colorColumn.getHeaderRenderer();
172
if (headerRenderer instanceof DefaultTableCellRenderer) {
173
((DefaultTableCellRenderer) headerRenderer).setToolTipText(
178
TableColumn vegetarianColumn = tableView.getColumn("Vegetarian");
179
vegetarianColumn.setPreferredWidth(100);
182
TableColumn numbersColumn = tableView.getColumn("Favorite Number");
183
@SuppressWarnings("serial")
184
DefaultTableCellRenderer numberColumnRenderer
185
= new DefaultTableCellRenderer() {
188
public void setValue(Object value) {
189
int cellValue = (value instanceof Number) ? ((Number) value).
191
setForeground((cellValue > 30) ? Color.black : Color.red);
192
setText((value == null) ? "" : value.toString());
195
numberColumnRenderer.setHorizontalAlignment(JLabel.RIGHT);
196
numbersColumn.setCellRenderer(numberColumnRenderer);
197
numbersColumn.setPreferredWidth(110);
200
JScrollPane scrollpane = new JScrollPane(tableView);
201
scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));
202
scrollpane.setPreferredSize(new Dimension(430, 200));
203
frame.getContentPane().add(scrollpane);
205
frame.setVisible(true);
208
public static void main(String[] args) {
211
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
212
if ("Nimbus".equals(info.getName())) {
213
UIManager.setLookAndFeel(info.getClassName());
217
} catch (Exception ex) {
218
Logger.getLogger(TableExample4.class.getName()).log(Level.SEVERE,
219
"Failed to apply Nimbus look and feel", ex);