Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JTable/4275046/bug4275046.java
41154 views
1
/*
2
* Copyright (c) 2014, 2020, 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
/*
25
* @test
26
* @key headful
27
* @bug 4275046
28
* @summary Tests editable combo box as a table editor.
29
* @run main bug4275046
30
*/
31
32
import java.awt.AWTException;
33
import java.awt.Point;
34
import java.awt.Rectangle;
35
import java.awt.Robot;
36
import java.awt.event.InputEvent;
37
import java.awt.event.KeyEvent;
38
import javax.swing.DefaultCellEditor;
39
import javax.swing.SwingUtilities;
40
import javax.swing.JComboBox;
41
import javax.swing.JFrame;
42
import javax.swing.JTable;
43
import javax.swing.table.DefaultTableModel;
44
45
public class bug4275046 {
46
47
private static final String[] colNames = { "ID", "Color", "Stuff" };
48
private static final Object[][] data = { { 1, "red", "abc"},
49
{ 2, "red", "def"},
50
{ 3, "red", "ghijk"} };
51
52
private static final String EXPECTED_VALUE = "rededited";
53
54
private JFrame frame;
55
private JTable table;
56
57
private volatile Point tableLoc;
58
private volatile Rectangle cellRect;
59
60
private volatile Object editedValue;
61
private volatile boolean testResult;
62
63
private final Robot robot;
64
65
public static void main(String[] args) throws Exception {
66
final bug4275046 test = new bug4275046();
67
test.test();
68
}
69
70
public bug4275046() throws AWTException {
71
robot = new Robot();
72
robot.setAutoDelay(100);
73
}
74
75
private void createGUI() {
76
frame = new JFrame("bug4275046");
77
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
78
79
JComboBox<Object> cb = new JComboBox<>(
80
new Object[] {"blue", "yellow", "green", "red"});
81
cb.setEditable(true);
82
DefaultCellEditor comboEditor = new DefaultCellEditor(cb);
83
comboEditor.setClickCountToStart(1);
84
85
DefaultTableModel model = new DefaultTableModel(data, colNames);
86
table = new JTable(model);
87
table.getColumnModel().getColumn(1).setCellEditor(comboEditor);
88
89
frame.add(table);
90
frame.pack();
91
frame.setSize(550, 400);
92
frame.setVisible(true);
93
}
94
95
private void test() throws Exception {
96
try {
97
SwingUtilities.invokeAndWait(new Runnable() {
98
@Override
99
public void run() {
100
createGUI();
101
}
102
});
103
104
runTest();
105
checkResult();
106
} finally {
107
SwingUtilities.invokeAndWait(new Runnable() {
108
@Override
109
public void run() {
110
if (frame != null) {
111
frame.dispose();
112
}
113
}
114
});
115
}
116
}
117
118
private void runTest() throws Exception {
119
robot.waitForIdle();
120
121
// Click the first cell in the "color" column
122
SwingUtilities.invokeAndWait(new Runnable() {
123
@Override
124
public void run() {
125
tableLoc = table.getLocationOnScreen();
126
cellRect = table.getCellRect(0, 1, true);
127
}
128
});
129
130
robot.mouseMove(tableLoc.x + cellRect.x + cellRect.width / 2,
131
tableLoc.y + cellRect.y + cellRect.height / 2);
132
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
133
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
134
robot.waitForIdle();
135
136
// Edit the cell
137
robot.keyPress(KeyEvent.VK_E);
138
robot.keyRelease(KeyEvent.VK_E);
139
140
robot.keyPress(KeyEvent.VK_D);
141
robot.keyRelease(KeyEvent.VK_D);
142
143
robot.keyPress(KeyEvent.VK_I);
144
robot.keyRelease(KeyEvent.VK_I);
145
146
robot.keyPress(KeyEvent.VK_T);
147
robot.keyRelease(KeyEvent.VK_T);
148
149
robot.keyPress(KeyEvent.VK_E);
150
robot.keyRelease(KeyEvent.VK_E);
151
152
robot.keyPress(KeyEvent.VK_D);
153
robot.keyRelease(KeyEvent.VK_D);
154
robot.delay(100);
155
156
// Click another cell
157
SwingUtilities.invokeAndWait(new Runnable() {
158
@Override
159
public void run() {
160
cellRect = table.getCellRect(1, 2, true);
161
}
162
});
163
164
robot.mouseMove(tableLoc.x + cellRect.x + cellRect.width / 2,
165
tableLoc.y + cellRect.y + cellRect.height / 2);
166
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
167
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
168
robot.delay(100);
169
}
170
171
private void checkResult() throws Exception {
172
robot.waitForIdle();
173
SwingUtilities.invokeAndWait(new Runnable() {
174
@Override
175
public void run() {
176
// Read the edited value of from the cell
177
editedValue = table.getModel().getValueAt(0, 1);
178
System.out.println("The edited value is = " + editedValue);
179
testResult = editedValue.equals(EXPECTED_VALUE);
180
if (testResult) {
181
System.out.println("Test passed");
182
} else {
183
System.out.println("Test failed");
184
}
185
}
186
});
187
if (!testResult) {
188
throw new RuntimeException("Expected value in the cell: '" +
189
EXPECTED_VALUE + "' but found '" + editedValue + "'.");
190
}
191
}
192
}
193
194