Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/demo/share/jfc/TableExample/TableExample4.java
41149 views
1
/*
2
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. 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 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.
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
* 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
37
* this sample code.
38
*/
39
40
41
42
import javax.swing.*;
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;
48
import java.awt.Color;
49
import java.util.logging.Level;
50
import java.util.logging.Logger;
51
import javax.swing.UIManager.LookAndFeelInfo;
52
53
54
/**
55
* Another JTable example, showing how column attributes can be refined
56
* even when columns have been created automatically. Here we create some
57
* specialized renderers and editors as well as changing widths and colors
58
* for some of the columns in the SwingSet demo table.
59
*
60
* @author Philip Milne
61
*/
62
public class TableExample4 {
63
64
public TableExample4() {
65
JFrame frame = new JFrame("Table");
66
frame.addWindowListener(new WindowAdapter() {
67
68
@Override
69
public void windowClosing(WindowEvent e) {
70
System.exit(0);
71
}
72
});
73
74
// Take the dummy data from SwingSet.
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 }
99
};
100
101
// Create a model of the data.
102
@SuppressWarnings("serial")
103
TableModel dataModel = new AbstractTableModel() {
104
// These methods always need to be implemented.
105
106
public int getColumnCount() {
107
return names.length;
108
}
109
110
public int getRowCount() {
111
return data.length;
112
}
113
114
public Object getValueAt(int row, int col) {
115
return data[row][col];
116
}
117
118
// The default implementations of these methods in
119
// AbstractTableModel would work, but we can refine them.
120
@Override
121
public String getColumnName(int column) {
122
return names[column];
123
}
124
125
@Override
126
public Class<?> getColumnClass(int c) {
127
return getValueAt(0, c).getClass();
128
}
129
130
@Override
131
public boolean isCellEditable(int row, int col) {
132
return true;
133
}
134
135
@Override
136
public void setValueAt(Object aValue, int row, int column) {
137
System.out.println("Setting value to: " + aValue);
138
data[row][column] = aValue;
139
}
140
};
141
142
// Create the table
143
JTable tableView = new JTable(dataModel);
144
// Turn off auto-resizing so that we can set column sizes
145
// programmatically. In this mode, all columns will get their preferred
146
// widths, as set blow.
147
tableView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
148
149
// Create a combo box to show that you can use one in a table.
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");
158
159
TableColumn colorColumn = tableView.getColumn("Favorite Color");
160
// Use the combo box as the editor in the "Favorite Color" column.
161
colorColumn.setCellEditor(new DefaultCellEditor(comboBox));
162
163
// Set a pink background and tooltip for the Color column renderer.
164
DefaultTableCellRenderer colorColumnRenderer =
165
new DefaultTableCellRenderer();
166
colorColumnRenderer.setBackground(Color.pink);
167
colorColumnRenderer.setToolTipText("Click for combo box");
168
colorColumn.setCellRenderer(colorColumnRenderer);
169
170
// Set a tooltip for the header of the colors column.
171
TableCellRenderer headerRenderer = colorColumn.getHeaderRenderer();
172
if (headerRenderer instanceof DefaultTableCellRenderer) {
173
((DefaultTableCellRenderer) headerRenderer).setToolTipText(
174
"Hi Mom!");
175
}
176
177
// Set the width of the "Vegetarian" column.
178
TableColumn vegetarianColumn = tableView.getColumn("Vegetarian");
179
vegetarianColumn.setPreferredWidth(100);
180
181
// Show the values in the "Favorite Number" column in different colors.
182
TableColumn numbersColumn = tableView.getColumn("Favorite Number");
183
@SuppressWarnings("serial")
184
DefaultTableCellRenderer numberColumnRenderer
185
= new DefaultTableCellRenderer() {
186
187
@Override
188
public void setValue(Object value) {
189
int cellValue = (value instanceof Number) ? ((Number) value).
190
intValue() : 0;
191
setForeground((cellValue > 30) ? Color.black : Color.red);
192
setText((value == null) ? "" : value.toString());
193
}
194
};
195
numberColumnRenderer.setHorizontalAlignment(JLabel.RIGHT);
196
numbersColumn.setCellRenderer(numberColumnRenderer);
197
numbersColumn.setPreferredWidth(110);
198
199
// Finish setting up the table.
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);
204
frame.pack();
205
frame.setVisible(true);
206
}
207
208
public static void main(String[] args) {
209
// Trying to set Nimbus look and feel
210
try {
211
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
212
if ("Nimbus".equals(info.getName())) {
213
UIManager.setLookAndFeel(info.getClassName());
214
break;
215
}
216
}
217
} catch (Exception ex) {
218
Logger.getLogger(TableExample4.class.getName()).log(Level.SEVERE,
219
"Failed to apply Nimbus look and feel", ex);
220
}
221
222
new TableExample4();
223
}
224
}
225
226