Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JTable/TAB/TAB.java
41153 views
1
/*
2
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.awt.Color;
25
import java.awt.Container;
26
import java.awt.Dimension;
27
28
import javax.swing.DefaultCellEditor;
29
import javax.swing.JApplet;
30
import javax.swing.JComboBox;
31
import javax.swing.JLabel;
32
import javax.swing.JScrollPane;
33
import javax.swing.JTable;
34
import javax.swing.SwingUtilities;
35
import javax.swing.UIManager;
36
import javax.swing.border.BevelBorder;
37
import javax.swing.table.AbstractTableModel;
38
import javax.swing.table.DefaultTableCellRenderer;
39
import javax.swing.table.TableCellRenderer;
40
import javax.swing.table.TableColumn;
41
import javax.swing.table.TableModel;
42
43
/**
44
* @test
45
* @bug 4128521
46
* @summary
47
* Tabbing test
48
* @author milne
49
* @run applet/manual=yesno TAB.html
50
*/
51
public class TAB extends JApplet
52
{
53
static void initTest(Container contentPane)
54
{
55
// Take the dummy data from SwingSet.
56
final String[] names = {"First Name", "Last Name", "Favorite Color",
57
"Favorite Number", "Vegetarian"};
58
final Object[][] data = {
59
{"Mark", "Andrews", "Red", new Integer(2), new Boolean(true)},
60
{"Tom", "Ball", "Blue", new Integer(99), new Boolean(false)},
61
{"Alan", "Chung", "Green", new Integer(838), new Boolean(false)},
62
{"Jeff", "Dinkins", "Turquois", new Integer(8), new Boolean(true)},
63
{"Amy", "Fowler", "Yellow", new Integer(3), new Boolean(false)},
64
{"Brian", "Gerhold", "Green", new Integer(0), new Boolean(false)},
65
{"James", "Gosling", "Pink", new Integer(21), new Boolean(false)},
66
{"David", "Karlton", "Red", new Integer(1), new Boolean(false)},
67
{"Dave", "Kloba", "Yellow", new Integer(14), new Boolean(false)},
68
{"Peter", "Korn", "Purple", new Integer(12), new Boolean(false)},
69
{"Phil", "Milne", "Purple", new Integer(3), new Boolean(false)},
70
{"Dave", "Moore", "Green", new Integer(88), new Boolean(false)},
71
{"Hans", "Muller", "Maroon", new Integer(5), new Boolean(false)},
72
{"Rick", "Levenson", "Blue", new Integer(2), new Boolean(false)},
73
{"Tim", "Prinzing", "Blue", new Integer(22), new Boolean(false)},
74
{"Chester", "Rose", "Black", new Integer(0), new Boolean(false)},
75
{"Ray", "Ryan", "Gray", new Integer(77), new Boolean(false)},
76
{"Georges", "Saab", "Red", new Integer(4), new Boolean(false)},
77
{"Willie", "Walker", "Phthalo Blue", new Integer(4), new Boolean(false)},
78
{"Kathy", "Walrath", "Blue", new Integer(8), new Boolean(false)},
79
{"Arnaud", "Weber", "Green", new Integer(44), new Boolean(false)}
80
};
81
82
// Create a model of the data.
83
TableModel dataModel = new AbstractTableModel() {
84
// These methods always need to be implemented.
85
public int getColumnCount() { return names.length; }
86
public int getRowCount() { return data.length;}
87
public Object getValueAt(int row, int col) {return data[row][col];}
88
89
// The default implementations of these methods in
90
// AbstractTableModel would work, but we can refine them.
91
public String getColumnName(int column) {return names[column];}
92
public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
93
public boolean isCellEditable(int row, int col) {return true;}
94
public void setValueAt(Object aValue, int row, int column) {
95
System.out.println("Setting value to: " + aValue);
96
data[row][column] = aValue;
97
}
98
};
99
100
// Create the table
101
JTable tableView = new JTable(dataModel);
102
// Turn off auto-resizing so that we can set column sizes programmatically.
103
// In this mode, all columns will get their preferred widths, as set blow.
104
tableView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
105
106
// Create a combo box to show that you can use one in a table.
107
JComboBox comboBox = new JComboBox();
108
comboBox.addItem("Red");
109
comboBox.addItem("Orange");
110
comboBox.addItem("Yellow");
111
comboBox.addItem("Green");
112
comboBox.addItem("Blue");
113
comboBox.addItem("Indigo");
114
comboBox.addItem("Violet");
115
116
TableColumn colorColumn = tableView.getColumn("Favorite Color");
117
// Use the combo box as the editor in the "Favorite Color" column.
118
colorColumn.setCellEditor(new DefaultCellEditor(comboBox));
119
120
// Set a pink background and tooltip for the Color column renderer.
121
DefaultTableCellRenderer colorColumnRenderer = new DefaultTableCellRenderer();
122
colorColumnRenderer.setBackground(Color.pink);
123
colorColumnRenderer.setToolTipText("Click for combo box");
124
colorColumn.setCellRenderer(colorColumnRenderer);
125
126
// Set a tooltip for the header of the colors column.
127
TableCellRenderer headerRenderer = colorColumn.getHeaderRenderer();
128
if (headerRenderer instanceof DefaultTableCellRenderer)
129
((DefaultTableCellRenderer)headerRenderer).setToolTipText("Hi Mom!");
130
131
// Set the width of the "Vegetarian" column.
132
TableColumn vegetarianColumn = tableView.getColumn("Vegetarian");
133
vegetarianColumn.setPreferredWidth(100);
134
135
// Show the values in the "Favorite Number" column in different colors.
136
TableColumn numbersColumn = tableView.getColumn("Favorite Number");
137
DefaultTableCellRenderer numberColumnRenderer = new DefaultTableCellRenderer() {
138
public void setValue(Object value) {
139
int cellValue = (value instanceof Number) ? ((Number)value).intValue() : 0;
140
setForeground((cellValue > 30) ? Color.black : Color.red);
141
setText((value == null) ? "" : value.toString());
142
}
143
};
144
numberColumnRenderer.setHorizontalAlignment(JLabel.RIGHT);
145
numbersColumn.setCellRenderer(numberColumnRenderer);
146
numbersColumn.setPreferredWidth(110);
147
148
// Finish setting up the table.
149
JScrollPane scrollpane = new JScrollPane(tableView);
150
scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));
151
scrollpane.setPreferredSize(new Dimension(430, 200));
152
153
contentPane.add(scrollpane);
154
}
155
156
157
public void init() {
158
SwingUtilities.invokeLater(() -> {
159
try {
160
UIManager.setLookAndFeel(
161
"javax.swing.plaf.metal.MetalLookAndFeel");
162
} catch (Exception e) {
163
throw new RuntimeException(e);
164
}
165
166
initTest(getContentPane());
167
});
168
}
169
}
170
171