29
29
* @author Shannon Hickey
30
30
* @run main bug6263446
31
31
*/
32
- import java .awt .*;
33
- import java .awt .event .*;
34
- import javax .swing .*;
35
- import javax .swing .table .*;
32
+ import java .awt .Point ;
33
+ import java .awt .Robot ;
34
+ import java .awt .Rectangle ;
35
+ import java .awt .event .InputEvent ;
36
+ import javax .swing .DefaultCellEditor ;
37
+ import javax .swing .JFrame ;
38
+ import javax .swing .JTable ;
39
+ import javax .swing .JTextField ;
40
+ import javax .swing .SwingUtilities ;
41
+ import javax .swing .table .TableModel ;
42
+ import javax .swing .table .DefaultTableModel ;
36
43
37
44
public class bug6263446 {
38
45
46
+ private static JFrame frame ;
39
47
private static JTable table ;
40
48
private static final String FIRST = "AAAAA" ;
41
49
private static final String SECOND = "BB" ;
42
50
private static final String ALL = FIRST + " " + SECOND ;
43
51
private static Robot robot ;
44
52
45
53
public static void main (String [] args ) throws Exception {
46
- robot = new Robot ();
47
- robot .setAutoDelay (50 );
54
+ try {
55
+ robot = new Robot ();
56
+ robot .setAutoDelay (50 );
48
57
49
- SwingUtilities .invokeAndWait (new Runnable () {
50
-
51
- public void run () {
52
- createAndShowGUI ();
53
- }
54
- });
58
+ SwingUtilities .invokeAndWait (() -> createAndShowGUI ());
59
+ robot .waitForIdle ();
60
+ robot .delay (1000 );
55
61
62
+ Point point = getClickPoint ();
63
+ robot .mouseMove (point .x , point .y );
64
+ robot .waitForIdle ();
56
65
57
- robot .waitForIdle ();
58
-
59
- Point point = getClickPoint ();
60
- robot .mouseMove (point .x , point .y );
61
- robot .waitForIdle ();
62
-
63
- click (1 );
64
- robot .waitForIdle ();
65
- assertEditing (false );
66
-
67
- click (2 );
68
- robot .waitForIdle ();
69
- checkSelectedText (null );
66
+ click (1 );
67
+ robot .waitForIdle ();
68
+ assertEditing (false );
70
69
71
- click (3 );
72
- robot .waitForIdle ();
73
- checkSelectedText (FIRST );
70
+ click (2 );
71
+ robot .waitForIdle ();
72
+ checkSelectedText (null );
74
73
74
+ click (3 );
75
+ robot .waitForIdle ();
76
+ checkSelectedText (FIRST );
75
77
76
- click (4 );
77
- robot .waitForIdle ();
78
- checkSelectedText (ALL );
78
+ click (4 );
79
+ robot .waitForIdle ();
80
+ checkSelectedText (ALL );
79
81
80
- setClickCountToStart (1 );
82
+ setClickCountToStart (1 );
83
+ robot .waitForIdle ();
81
84
82
- click (1 );
83
- robot .waitForIdle ();
84
- checkSelectedText (null );
85
+ click (1 );
86
+ robot .waitForIdle ();
87
+ checkSelectedText (null );
85
88
86
- click (2 );
87
- robot .waitForIdle ();
88
- checkSelectedText (FIRST );
89
+ click (2 );
90
+ robot .waitForIdle ();
91
+ checkSelectedText (FIRST );
89
92
90
- click (3 );
91
- robot .waitForIdle ();
92
- checkSelectedText (ALL );
93
+ click (3 );
94
+ robot .waitForIdle ();
95
+ checkSelectedText (ALL );
93
96
94
- setClickCountToStart (3 );
97
+ setClickCountToStart (3 );
98
+ robot .waitForIdle ();
95
99
96
- click (1 );
97
- robot .waitForIdle ();
98
- assertEditing (false );
100
+ click (1 );
101
+ robot .waitForIdle ();
102
+ assertEditing (false );
99
103
100
- click (2 );
101
- robot .waitForIdle ();
102
- assertEditing (false );
104
+ click (2 );
105
+ robot .waitForIdle ();
106
+ assertEditing (false );
103
107
104
- click (3 );
105
- robot .waitForIdle ();
106
- checkSelectedText (null );
108
+ click (3 );
109
+ robot .waitForIdle ();
110
+ checkSelectedText (null );
107
111
108
- click (4 );
109
- robot .waitForIdle ();
110
- checkSelectedText (FIRST );
112
+ click (4 );
113
+ robot .waitForIdle ();
114
+ checkSelectedText (FIRST );
111
115
112
- click (5 );
113
- robot .waitForIdle ();
114
- checkSelectedText (ALL );
116
+ click (5 );
117
+ robot .waitForIdle ();
118
+ checkSelectedText (ALL );
115
119
120
+ SwingUtilities .invokeAndWait (() -> table .editCellAt (0 , 0 ));
116
121
117
- SwingUtilities .invokeAndWait (new Runnable () {
122
+ robot .waitForIdle ();
123
+ assertEditing (true );
118
124
119
- @ Override
120
- public void run () {
121
- table .editCellAt (0 , 0 );
125
+ click (2 );
126
+ robot .waitForIdle ();
127
+ checkSelectedText (FIRST );
128
+ } finally {
129
+ if (frame != null ) {
130
+ SwingUtilities .invokeAndWait (frame ::dispose );
122
131
}
123
- });
124
-
125
- robot .waitForIdle ();
126
- assertEditing (true );
127
-
128
- click (2 );
129
- robot .waitForIdle ();
130
- checkSelectedText (FIRST );
131
-
132
+ }
132
133
}
133
134
134
135
private static void checkSelectedText (String sel ) throws Exception {
135
136
assertEditing (true );
136
137
checkSelection (sel );
138
+ robot .waitForIdle ();
137
139
cancelCellEditing ();
140
+ robot .waitForIdle ();
138
141
assertEditing (false );
139
142
}
140
143
141
144
private static void setClickCountToStart (final int clicks ) throws Exception {
142
- SwingUtilities .invokeAndWait (new Runnable () {
143
-
144
- @ Override
145
- public void run () {
146
- DefaultCellEditor editor =
147
- (DefaultCellEditor ) table .getDefaultEditor (String .class );
148
- editor .setClickCountToStart (clicks );
149
- }
145
+ SwingUtilities .invokeAndWait (() -> {
146
+ DefaultCellEditor editor =
147
+ (DefaultCellEditor ) table .getDefaultEditor (String .class );
148
+ editor .setClickCountToStart (clicks );
150
149
});
151
-
152
150
}
153
151
154
152
private static void cancelCellEditing () throws Exception {
155
- SwingUtilities .invokeAndWait (new Runnable () {
156
-
157
- @ Override
158
- public void run () {
159
- table .getCellEditor ().cancelCellEditing ();
160
- }
153
+ SwingUtilities .invokeAndWait (() -> {
154
+ table .getCellEditor ().cancelCellEditing ();
161
155
});
162
156
}
163
157
164
158
private static void checkSelection (final String sel ) throws Exception {
165
- SwingUtilities .invokeAndWait (new Runnable () {
166
-
167
- @ Override
168
- public void run () {
169
- DefaultCellEditor editor =
170
- (DefaultCellEditor ) table .getDefaultEditor (String .class );
171
- JTextField field = (JTextField ) editor .getComponent ();
172
- String text = field .getSelectedText ();
173
- if (sel == null ) {
174
- if (text != null && text .length () != 0 ) {
175
- throw new RuntimeException ("Nothing should be selected,"
176
- + " but \" " + text + "\" is selected." );
177
- }
178
- } else if (!sel .equals (text )) {
179
- throw new RuntimeException ("\" " + sel + "\" should be "
180
- + "selected, but \" " + text + "\" is selected." );
159
+ SwingUtilities .invokeAndWait (() -> {
160
+ DefaultCellEditor editor =
161
+ (DefaultCellEditor ) table .getDefaultEditor (String .class );
162
+ JTextField field = (JTextField ) editor .getComponent ();
163
+ String text = field .getSelectedText ();
164
+ if (sel == null ) {
165
+ if (text != null && text .length () != 0 ) {
166
+ throw new RuntimeException ("Nothing should be selected,"
167
+ + " but \" " + text + "\" is selected." );
181
168
}
169
+ } else if (!sel .equals (text )) {
170
+ throw new RuntimeException ("\" " + sel + "\" should be "
171
+ + "selected, but \" " + text + "\" is selected." );
182
172
}
183
173
});
184
174
}
185
175
186
176
private static void assertEditing (final boolean editing ) throws Exception {
187
- SwingUtilities .invokeAndWait (new Runnable () {
188
-
189
- @ Override
190
- public void run () {
191
- if (editing && !table .isEditing ()) {
192
- throw new RuntimeException ("Table should be editing" );
193
- }
194
- if (!editing && table .isEditing ()) {
195
- throw new RuntimeException ("Table should not be editing" );
196
- }
177
+ SwingUtilities .invokeAndWait (() -> {
178
+ if (editing && !table .isEditing ()) {
179
+ throw new RuntimeException ("Table should be editing" );
180
+ }
181
+ if (!editing && table .isEditing ()) {
182
+ throw new RuntimeException ("Table should not be editing" );
197
183
}
198
184
});
199
185
}
200
186
201
187
private static Point getClickPoint () throws Exception {
202
188
final Point [] result = new Point [1 ];
203
- SwingUtilities .invokeAndWait (new Runnable () {
204
-
205
- @ Override
206
- public void run () {
207
- Rectangle rect = table .getCellRect (0 , 0 , false );
208
- Point point = new Point (rect .x + rect .width / 5 ,
209
- rect .y + rect .height / 2 );
210
- SwingUtilities .convertPointToScreen (point , table );
211
- result [0 ] = point ;
212
- }
189
+ SwingUtilities .invokeAndWait (() -> {
190
+ Rectangle rect = table .getCellRect (0 , 0 , false );
191
+ Point point = new Point (rect .x + rect .width / 5 ,
192
+ rect .y + rect .height / 2 );
193
+ SwingUtilities .convertPointToScreen (point , table );
194
+ result [0 ] = point ;
213
195
});
214
-
215
196
return result [0 ];
216
197
}
217
198
218
199
private static void click (int times ) {
219
200
robot .delay (500 );
220
201
for (int i = 0 ; i < times ; i ++) {
221
- robot .mousePress (InputEvent .BUTTON1_MASK );
222
- robot .mouseRelease (InputEvent .BUTTON1_MASK );
202
+ robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
203
+ robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
223
204
}
224
205
}
225
206
@@ -231,11 +212,14 @@ private static TableModel createTableModel() {
231
212
}
232
213
233
214
private static void createAndShowGUI () {
234
- JFrame frame = new JFrame ("bug6263446" );
215
+ frame = new JFrame ("bug6263446" );
235
216
frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
236
217
table = new JTable (createTableModel ());
237
218
frame .add (table );
219
+ frame .setAlwaysOnTop (true );
220
+ frame .setLocationRelativeTo (null );
238
221
frame .pack ();
239
222
frame .setVisible (true );
223
+ frame .toFront ();
240
224
}
241
225
}
0 commit comments