3535import java .awt .event .KeyEvent ;
3636
3737import javax .swing .BorderFactory ;
38- import javax .swing .BoxLayout ;
38+ import javax .swing .Box ;
3939import javax .swing .ButtonGroup ;
4040import javax .swing .JButton ;
4141import javax .swing .JFrame ;
42- import javax .swing .JPanel ;
4342import javax .swing .JRadioButton ;
4443import javax .swing .SwingUtilities ;
4544import javax .swing .UIManager ;
@@ -55,11 +54,14 @@ public class bug8033699 {
5554 private static JRadioButton radioBtn2 ;
5655 private static JRadioButton radioBtn3 ;
5756 private static JRadioButton radioBtnSingle ;
57+ private static KeyboardFocusManager focusManager ;
5858
5959 public static void main (String [] args ) throws Throwable {
6060 robot = new Robot ();
6161
62- // Get all installed Look and Feels
62+ SwingUtilities .invokeAndWait (() ->
63+ focusManager = KeyboardFocusManager .getCurrentKeyboardFocusManager ());
64+
6365 UIManager .LookAndFeelInfo [] lafs = UIManager .getInstalledLookAndFeels ();
6466 for (UIManager .LookAndFeelInfo laf : lafs ) {
6567 testLaF (laf );
@@ -97,23 +99,29 @@ private static void testLaF(UIManager.LookAndFeelInfo laf) throws Exception {
9799 runTest5 ();
98100 robot .delay (100 );
99101
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
101104 runTest6 ();
102105 robot .delay (100 );
103106
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
105109 runTest7 ();
106110 robot .delay (100 );
107111
108112 // down key circle back to first button in grouped radio button
109113 runTest8 ();
110114 robot .delay (100 );
111115
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
113118 runTest9 ();
114119 robot .delay (100 );
115120 } 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 );
117125 } finally {
118126 SwingUtilities .invokeAndWait (() -> {
119127 if (mainFrame != null ) {
@@ -135,13 +143,12 @@ private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
135143 }
136144
137145 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" );
139147 btnStart = new JButton ("Start" );
140148 btnEnd = new JButton ("End" );
141149 btnMiddle = new JButton ("Middle" );
142150
143- JPanel box = new JPanel ();
144- box .setLayout (new BoxLayout (box , BoxLayout .Y_AXIS ));
151+ Box box = Box .createVerticalBox ();
145152 box .setBorder (BorderFactory .createTitledBorder ("Grouped Radio Buttons" ));
146153 radioBtn1 = new JRadioButton ("A" );
147154 radioBtn2 = new JRadioButton ("B" );
@@ -161,16 +168,17 @@ private static void createAndShowGUI() {
161168 radioBtnSingle = new JRadioButton ("Not Grouped" );
162169 radioBtnSingle .setSelected (true );
163170
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 );
168176
177+ mainFrame .add (mainBox );
169178 mainFrame .getRootPane ().setDefaultButton (btnStart );
170179 btnStart .requestFocus ();
171180
172181 mainFrame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
173- mainFrame .setLayout (new BoxLayout (mainFrame .getContentPane (), BoxLayout .Y_AXIS ));
174182
175183 mainFrame .setSize (300 , 300 );
176184 mainFrame .setLocationRelativeTo (null );
@@ -185,33 +193,41 @@ private static void runTest1() throws Exception {
185193 hitKey (KeyEvent .VK_TAB );
186194
187195 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" );
191201 }
192202 });
193203 }
194204
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
196207 private static void runTest2 () throws Exception {
197208 hitKey (KeyEvent .VK_TAB );
198209 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" );
202215 }
203216 });
204217 }
205218
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
207221 private static void runTest3 () throws Exception {
208222 hitKey (KeyEvent .VK_SHIFT , KeyEvent .VK_TAB );
209223 hitKey (KeyEvent .VK_SHIFT , KeyEvent .VK_TAB );
210224 hitKey (KeyEvent .VK_SHIFT , KeyEvent .VK_TAB );
211225 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" );
215231 }
216232 });
217233 }
@@ -221,9 +237,11 @@ private static void runTest4() throws Exception {
221237 hitKey (KeyEvent .VK_DOWN );
222238 hitKey (KeyEvent .VK_RIGHT );
223239 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" );
227245 }
228246 });
229247 }
@@ -232,9 +250,11 @@ private static void runTest5() throws Exception {
232250 hitKey (KeyEvent .VK_UP );
233251 hitKey (KeyEvent .VK_LEFT );
234252 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" );
238258 }
239259 });
240260 }
@@ -243,38 +263,45 @@ private static void runTest6() throws Exception {
243263 hitKey (KeyEvent .VK_UP );
244264 hitKey (KeyEvent .VK_UP );
245265 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" );
249271 }
250272 });
251273 }
252274
253275 private static void runTest7 () throws Exception {
254276 hitKey (KeyEvent .VK_TAB );
255277 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" );
259283 }
260284 });
261285 }
262286
263287 private static void runTest8 () throws Exception {
264288 hitKey (KeyEvent .VK_TAB );
265289 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" );
269295 }
270296 });
271297 }
272298
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 ;
276302
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
278305 private static void runTest9 () throws Exception {
279306 SwingUtilities .invokeAndWait (() -> {
280307 radioBtn1 .setSelected (true );
@@ -285,15 +312,19 @@ private static void runTest9() throws Exception {
285312 ActionListener actLrRB2 = e -> actRB2 = true ;
286313 ActionListener actLrRB3 = e -> actRB3 = true ;
287314
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+ });
291321
292322 hitKey (KeyEvent .VK_DOWN );
293323 hitKey (KeyEvent .VK_DOWN );
294324 hitKey (KeyEvent .VK_DOWN );
295325
296- String failMessage = "ActionListener not invoked when selected using arrow key." ;
326+ String failMessage = "ActionListener not invoked when selected using "
327+ + "arrow key." ;
297328 if (!actRB2 ) {
298329 throw new RuntimeException ("RadioButton 2: " + failMessage );
299330 }
@@ -304,9 +335,12 @@ private static void runTest9() throws Exception {
304335 throw new RuntimeException ("RadioButton 1: " + failMessage );
305336 }
306337
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+ });
310344 }
311345
312346 private static void hitKey (int keycode ) {
0 commit comments