1 /*
2  * Copyright (c) 2016, 2017, 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 /* @test
25  * @key headful
26  * @bug 8074883
27  * @summary Tab key should move to focused button in a button group
28  * @run main ButtonGroupFocusTest
29  */
30 
31 import javax.swing.*;
32 import java.awt.*;
33 import java.awt.event.KeyEvent;
34 
35 public class ButtonGroupFocusTest {
36 
37     private static JRadioButton button1;
38     private static JRadioButton button2;
39     private static JRadioButton button3;
40     private static JRadioButton button4;
41     private static JRadioButton button5;
42     private static Robot robot;
43     private static JFrame frame;
44 
main(String[] args)45     public static void main(String[] args) throws Exception {
46         robot = new Robot();
47         robot.setAutoDelay(100);
48 
49         SwingUtilities.invokeAndWait(() -> {
50             frame = new JFrame();
51             Container contentPane = frame.getContentPane();
52             contentPane.setLayout(new FlowLayout());
53             button1 = new JRadioButton("Button 1");
54             contentPane.add(button1);
55             button2 = new JRadioButton("Button 2");
56             contentPane.add(button2);
57             button3 = new JRadioButton("Button 3");
58             contentPane.add(button3);
59             button4 = new JRadioButton("Button 4");
60             contentPane.add(button4);
61             button5 = new JRadioButton("Button 5");
62             contentPane.add(button5);
63             ButtonGroup group = new ButtonGroup();
64             group.add(button1);
65             group.add(button2);
66             group.add(button3);
67 
68             group = new ButtonGroup();
69             group.add(button4);
70             group.add(button5);
71 
72             button2.setSelected(true);
73 
74             frame.pack();
75             frame.setVisible(true);
76         });
77 
78         robot.waitForIdle();
79         robot.delay(200);
80 
81         SwingUtilities.invokeAndWait(() -> {
82             if( !button2.hasFocus() ) {
83                 frame.dispose();
84                 throw new RuntimeException(
85                         "Button 2 should get focus after activation");
86             }
87         });
88 
89         robot.keyPress(KeyEvent.VK_TAB);
90         robot.keyRelease(KeyEvent.VK_TAB);
91 
92         robot.waitForIdle();
93         robot.delay(200);
94 
95         SwingUtilities.invokeAndWait(() -> {
96             if( !button4.hasFocus() ) {
97                 frame.dispose();
98                 throw new RuntimeException(
99                         "Button 4 should get focus");
100             }
101             button3.setSelected(true);
102         });
103 
104         robot.keyPress(KeyEvent.VK_TAB);
105         robot.keyRelease(KeyEvent.VK_TAB);
106 
107         robot.waitForIdle();
108         robot.delay(200);
109 
110         SwingUtilities.invokeAndWait(() -> {
111             if( !button3.hasFocus() ) {
112                 frame.dispose();
113                 throw new RuntimeException(
114                         "selected Button 3 should get focus");
115             }
116         });
117 
118         SwingUtilities.invokeLater(frame::dispose);
119     }
120 }
121