35
35
import java .awt .event .KeyEvent ;
36
36
37
37
import javax .swing .BorderFactory ;
38
- import javax .swing .BoxLayout ;
38
+ import javax .swing .Box ;
39
39
import javax .swing .ButtonGroup ;
40
40
import javax .swing .JButton ;
41
41
import javax .swing .JFrame ;
42
- import javax .swing .JPanel ;
43
42
import javax .swing .JRadioButton ;
44
43
import javax .swing .SwingUtilities ;
45
44
import javax .swing .UIManager ;
@@ -55,11 +54,14 @@ public class bug8033699 {
55
54
private static JRadioButton radioBtn2 ;
56
55
private static JRadioButton radioBtn3 ;
57
56
private static JRadioButton radioBtnSingle ;
57
+ private static KeyboardFocusManager focusManager ;
58
58
59
59
public static void main (String [] args ) throws Throwable {
60
60
robot = new Robot ();
61
61
62
- // Get all installed Look and Feels
62
+ SwingUtilities .invokeAndWait (() ->
63
+ focusManager = KeyboardFocusManager .getCurrentKeyboardFocusManager ());
64
+
63
65
UIManager .LookAndFeelInfo [] lafs = UIManager .getInstalledLookAndFeels ();
64
66
for (UIManager .LookAndFeelInfo laf : lafs ) {
65
67
testLaF (laf );
@@ -97,23 +99,29 @@ private static void testLaF(UIManager.LookAndFeelInfo laf) throws Exception {
97
99
runTest5 ();
98
100
robot .delay (100 );
99
101
100
- // tab from radio button in group to next component in the middle of button group layout
102
+ // tab from radio button in group to next component in the middle of
103
+ // button group layout
101
104
runTest6 ();
102
105
robot .delay (100 );
103
106
104
- // tab to radio button in group from component in the middle of button group layout
107
+ // tab to radio button in group from component in the middle of
108
+ // button group layout
105
109
runTest7 ();
106
110
robot .delay (100 );
107
111
108
112
// down key circle back to first button in grouped radio button
109
113
runTest8 ();
110
114
robot .delay (100 );
111
115
112
- // Verify that ActionListener is called when a RadioButton is selected using arrow key.
116
+ // Verify that ActionListener is called when a RadioButton is
117
+ // selected using arrow key
113
118
runTest9 ();
114
119
robot .delay (100 );
115
120
} catch (Exception e ) {
116
- throw new RuntimeException ("Error testing LaF: " + laf .getName (), e );
121
+ Throwable cause = e .getCause ();
122
+ throw new RuntimeException ("Error testing LaF: " + laf .getName ()
123
+ + (cause != null ? " - " + cause .getMessage () : "" ),
124
+ e );
117
125
} finally {
118
126
SwingUtilities .invokeAndWait (() -> {
119
127
if (mainFrame != null ) {
@@ -135,13 +143,12 @@ private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
135
143
}
136
144
137
145
private static void createAndShowGUI () {
138
- mainFrame = new JFrame ("Bug 8033699 - 8 Tests for Grouped/Non Group Radio Buttons " );
146
+ mainFrame = new JFrame ("Radio Button Focus Tests" );
139
147
btnStart = new JButton ("Start" );
140
148
btnEnd = new JButton ("End" );
141
149
btnMiddle = new JButton ("Middle" );
142
150
143
- JPanel box = new JPanel ();
144
- box .setLayout (new BoxLayout (box , BoxLayout .Y_AXIS ));
151
+ Box box = Box .createVerticalBox ();
145
152
box .setBorder (BorderFactory .createTitledBorder ("Grouped Radio Buttons" ));
146
153
radioBtn1 = new JRadioButton ("A" );
147
154
radioBtn2 = new JRadioButton ("B" );
@@ -161,16 +168,17 @@ private static void createAndShowGUI() {
161
168
radioBtnSingle = new JRadioButton ("Not Grouped" );
162
169
radioBtnSingle .setSelected (true );
163
170
164
- mainFrame .getContentPane ().add (btnStart );
165
- mainFrame .getContentPane ().add (box );
166
- mainFrame .getContentPane ().add (radioBtnSingle );
167
- mainFrame .getContentPane ().add (btnEnd );
171
+ Box mainBox = Box .createVerticalBox ();
172
+ mainBox .add (btnStart );
173
+ mainBox .add (box );
174
+ mainBox .add (radioBtnSingle );
175
+ mainBox .add (btnEnd );
168
176
177
+ mainFrame .add (mainBox );
169
178
mainFrame .getRootPane ().setDefaultButton (btnStart );
170
179
btnStart .requestFocus ();
171
180
172
181
mainFrame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
173
- mainFrame .setLayout (new BoxLayout (mainFrame .getContentPane (), BoxLayout .Y_AXIS ));
174
182
175
183
mainFrame .setSize (300 , 300 );
176
184
mainFrame .setLocationRelativeTo (null );
@@ -185,33 +193,41 @@ private static void runTest1() throws Exception {
185
193
hitKey (KeyEvent .VK_TAB );
186
194
187
195
SwingUtilities .invokeAndWait (() -> {
188
- if (KeyboardFocusManager .getCurrentKeyboardFocusManager ().getFocusOwner () != radioBtnSingle ) {
189
- System .out .println ("Radio Button Group Go To Next Component through Tab Key failed" );
190
- throw new RuntimeException ("Focus is not on Radio Button Single as Expected" );
196
+ if (focusManager .getFocusOwner () != radioBtnSingle ) {
197
+ System .out .println ("Radio Button Group Go To "
198
+ + "Next Component through Tab Key failed" );
199
+ throw new RuntimeException ("Focus is not on "
200
+ + "Radio Button Single as Expected" );
191
201
}
192
202
});
193
203
}
194
204
195
- // Non-Grouped Radio button as a single component when traversing through tab key
205
+ // Non-Grouped Radio button as a single component when traversing through
206
+ // tab key
196
207
private static void runTest2 () throws Exception {
197
208
hitKey (KeyEvent .VK_TAB );
198
209
SwingUtilities .invokeAndWait (() -> {
199
- if (KeyboardFocusManager .getCurrentKeyboardFocusManager ().getFocusOwner () != btnEnd ) {
200
- System .out .println ("Non Grouped Radio Button Go To Next Component through Tab Key failed" );
201
- throw new RuntimeException ("Focus is not on Button End as Expected" );
210
+ if (focusManager .getFocusOwner () != btnEnd ) {
211
+ System .out .println ("Non Grouped Radio Button Go To "
212
+ + "Next Component through Tab Key failed" );
213
+ throw new RuntimeException ("Focus is not on Button End "
214
+ + "as Expected" );
202
215
}
203
216
});
204
217
}
205
218
206
- // Non-Grouped Radio button and Group Radio button as a single component when traversing through shift-tab key
219
+ // Non-Grouped Radio button and Group Radio button as a single component
220
+ // when traversing through shift-tab key
207
221
private static void runTest3 () throws Exception {
208
222
hitKey (KeyEvent .VK_SHIFT , KeyEvent .VK_TAB );
209
223
hitKey (KeyEvent .VK_SHIFT , KeyEvent .VK_TAB );
210
224
hitKey (KeyEvent .VK_SHIFT , KeyEvent .VK_TAB );
211
225
SwingUtilities .invokeAndWait (() -> {
212
- if (KeyboardFocusManager .getCurrentKeyboardFocusManager ().getFocusOwner () != radioBtn1 ) {
213
- System .out .println ("Radio button Group/Non Grouped Radio Button SHIFT-Tab Key Test failed" );
214
- throw new RuntimeException ("Focus is not on Radio Button A as Expected" );
226
+ if (focusManager .getFocusOwner () != radioBtn1 ) {
227
+ System .out .println ("Radio button Group/Non Grouped "
228
+ + "Radio Button SHIFT-Tab Key Test failed" );
229
+ throw new RuntimeException ("Focus is not on Radio Button A "
230
+ + "as Expected" );
215
231
}
216
232
});
217
233
}
@@ -221,9 +237,11 @@ private static void runTest4() throws Exception {
221
237
hitKey (KeyEvent .VK_DOWN );
222
238
hitKey (KeyEvent .VK_RIGHT );
223
239
SwingUtilities .invokeAndWait (() -> {
224
- if (KeyboardFocusManager .getCurrentKeyboardFocusManager ().getFocusOwner () != radioBtn3 ) {
225
- System .out .println ("Radio button Group UP/LEFT Arrow Key Move Focus Failed" );
226
- throw new RuntimeException ("Focus is not on Radio Button C as Expected" );
240
+ if (focusManager .getFocusOwner () != radioBtn3 ) {
241
+ System .out .println ("Radio button Group UP/LEFT Arrow Key "
242
+ + "Move Focus Failed" );
243
+ throw new RuntimeException ("Focus is not on Radio Button C "
244
+ + "as Expected" );
227
245
}
228
246
});
229
247
}
@@ -232,9 +250,11 @@ private static void runTest5() throws Exception {
232
250
hitKey (KeyEvent .VK_UP );
233
251
hitKey (KeyEvent .VK_LEFT );
234
252
SwingUtilities .invokeAndWait (() -> {
235
- if (KeyboardFocusManager .getCurrentKeyboardFocusManager ().getFocusOwner () != radioBtn1 ) {
236
- System .out .println ("Radio button Group Left/Up Arrow Key Move Focus Failed" );
237
- throw new RuntimeException ("Focus is not on Radio Button A as Expected" );
253
+ if (focusManager .getFocusOwner () != radioBtn1 ) {
254
+ System .out .println ("Radio button Group Left/Up Arrow Key "
255
+ + "Move Focus Failed" );
256
+ throw new RuntimeException ("Focus is not on Radio Button A "
257
+ + "as Expected" );
238
258
}
239
259
});
240
260
}
@@ -243,38 +263,45 @@ private static void runTest6() throws Exception {
243
263
hitKey (KeyEvent .VK_UP );
244
264
hitKey (KeyEvent .VK_UP );
245
265
SwingUtilities .invokeAndWait (() -> {
246
- if (KeyboardFocusManager .getCurrentKeyboardFocusManager ().getFocusOwner () != radioBtn2 ) {
247
- System .out .println ("Radio button Group Circle Back To First Button Test" );
248
- throw new RuntimeException ("Focus is not on Radio Button B as Expected" );
266
+ if (focusManager .getFocusOwner () != radioBtn2 ) {
267
+ System .out .println ("Radio button Group Circle Back To "
268
+ + "First Button Test" );
269
+ throw new RuntimeException ("Focus is not on Radio Button B "
270
+ + "as Expected" );
249
271
}
250
272
});
251
273
}
252
274
253
275
private static void runTest7 () throws Exception {
254
276
hitKey (KeyEvent .VK_TAB );
255
277
SwingUtilities .invokeAndWait (() -> {
256
- if (KeyboardFocusManager .getCurrentKeyboardFocusManager ().getFocusOwner () != btnMiddle ) {
257
- System .out .println ("Separate Component added in button group layout" );
258
- throw new RuntimeException ("Focus is not on Middle Button as Expected" );
278
+ if (focusManager .getFocusOwner () != btnMiddle ) {
279
+ System .out .println ("Separate Component added in"
280
+ + " button group layout" );
281
+ throw new RuntimeException ("Focus is not on Middle Button"
282
+ + " as Expected" );
259
283
}
260
284
});
261
285
}
262
286
263
287
private static void runTest8 () throws Exception {
264
288
hitKey (KeyEvent .VK_TAB );
265
289
SwingUtilities .invokeAndWait (() -> {
266
- if (KeyboardFocusManager .getCurrentKeyboardFocusManager ().getFocusOwner () != radioBtnSingle ) {
267
- System .out .println ("Separate Component added in button group layout" );
268
- throw new RuntimeException ("Focus is not on Radio Button Single as Expected" );
290
+ if (focusManager .getFocusOwner () != radioBtnSingle ) {
291
+ System .out .println ("Separate Component added in"
292
+ + " button group layout" );
293
+ throw new RuntimeException ("Focus is not on Radio Button Single"
294
+ + " as Expected" );
269
295
}
270
296
});
271
297
}
272
298
273
- private static boolean actRB1 = false ;
274
- private static boolean actRB2 = false ;
275
- private static boolean actRB3 = false ;
299
+ private static volatile boolean actRB1 = false ;
300
+ private static volatile boolean actRB2 = false ;
301
+ private static volatile boolean actRB3 = false ;
276
302
277
- // JDK-8226892: Verify that ActionListener is called when a RadioButton is selected using arrow key.
303
+ // JDK-8226892: Verify that ActionListener is called when a RadioButton
304
+ // is selected using arrow key
278
305
private static void runTest9 () throws Exception {
279
306
SwingUtilities .invokeAndWait (() -> {
280
307
radioBtn1 .setSelected (true );
@@ -285,15 +312,19 @@ private static void runTest9() throws Exception {
285
312
ActionListener actLrRB2 = e -> actRB2 = true ;
286
313
ActionListener actLrRB3 = e -> actRB3 = true ;
287
314
288
- radioBtn1 .addActionListener (actLrRB1 );
289
- radioBtn2 .addActionListener (actLrRB2 );
290
- radioBtn3 .addActionListener (actLrRB3 );
315
+ // Adding Action Listeners
316
+ SwingUtilities .invokeAndWait (() -> {
317
+ radioBtn1 .addActionListener (actLrRB1 );
318
+ radioBtn2 .addActionListener (actLrRB2 );
319
+ radioBtn3 .addActionListener (actLrRB3 );
320
+ });
291
321
292
322
hitKey (KeyEvent .VK_DOWN );
293
323
hitKey (KeyEvent .VK_DOWN );
294
324
hitKey (KeyEvent .VK_DOWN );
295
325
296
- String failMessage = "ActionListener not invoked when selected using arrow key." ;
326
+ String failMessage = "ActionListener not invoked when selected using "
327
+ + "arrow key." ;
297
328
if (!actRB2 ) {
298
329
throw new RuntimeException ("RadioButton 2: " + failMessage );
299
330
}
@@ -304,9 +335,12 @@ private static void runTest9() throws Exception {
304
335
throw new RuntimeException ("RadioButton 1: " + failMessage );
305
336
}
306
337
307
- radioBtn1 .removeActionListener (actLrRB1 );
308
- radioBtn2 .removeActionListener (actLrRB2 );
309
- radioBtn3 .removeActionListener (actLrRB3 );
338
+ // Removing Action Listeners
339
+ SwingUtilities .invokeAndWait (() -> {
340
+ radioBtn1 .removeActionListener (actLrRB1 );
341
+ radioBtn2 .removeActionListener (actLrRB2 );
342
+ radioBtn3 .removeActionListener (actLrRB3 );
343
+ });
310
344
}
311
345
312
346
private static void hitKey (int keycode ) {
0 commit comments