KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TableExample4


1 /*
2  * @(#)TableExample4.java 1.19 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  * @(#)TableExample4.java 1.19 05/11/17
39  */

40
41 /**
42  * Another JTable example, showing how column attributes can be refined
43  * even when columns have been created automatically. Here we create some
44  * specialised renderers and editors as well as changing widths and colors
45  * for some of the columns in the SwingSet demo table.
46  *
47  * @version 1.19 11/17/05
48  * @author Philip Milne
49  */

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