Path: blob/master/test/jdk/javax/swing/JRadioButton/ButtonGroupFocus/ButtonGroupFocusTest.java
41153 views
/*1* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @key headful25* @bug 807488326* @summary Tab key should move to focused button in a button group27* @run main ButtonGroupFocusTest28*/2930import javax.swing.*;31import java.awt.*;32import java.awt.event.KeyEvent;3334public class ButtonGroupFocusTest {3536private static JRadioButton button1;37private static JRadioButton button2;38private static JRadioButton button3;39private static JRadioButton button4;40private static JRadioButton button5;41private static Robot robot;42private static JFrame frame;4344public static void main(String[] args) throws Exception {45robot = new Robot();46robot.setAutoDelay(100);4748SwingUtilities.invokeAndWait(() -> {49frame = new JFrame();50Container contentPane = frame.getContentPane();51contentPane.setLayout(new FlowLayout());52button1 = new JRadioButton("Button 1");53contentPane.add(button1);54button2 = new JRadioButton("Button 2");55contentPane.add(button2);56button3 = new JRadioButton("Button 3");57contentPane.add(button3);58button4 = new JRadioButton("Button 4");59contentPane.add(button4);60button5 = new JRadioButton("Button 5");61contentPane.add(button5);62ButtonGroup group = new ButtonGroup();63group.add(button1);64group.add(button2);65group.add(button3);6667group = new ButtonGroup();68group.add(button4);69group.add(button5);7071button2.setSelected(true);7273frame.pack();74frame.setVisible(true);75});7677robot.waitForIdle();78robot.delay(200);7980SwingUtilities.invokeAndWait(() -> {81if( !button2.hasFocus() ) {82frame.dispose();83throw new RuntimeException(84"Button 2 should get focus after activation");85}86});8788robot.keyPress(KeyEvent.VK_TAB);89robot.keyRelease(KeyEvent.VK_TAB);9091robot.waitForIdle();92robot.delay(200);9394SwingUtilities.invokeAndWait(() -> {95if( !button4.hasFocus() ) {96frame.dispose();97throw new RuntimeException(98"Button 4 should get focus");99}100button3.setSelected(true);101});102103robot.keyPress(KeyEvent.VK_TAB);104robot.keyRelease(KeyEvent.VK_TAB);105106robot.waitForIdle();107robot.delay(200);108109SwingUtilities.invokeAndWait(() -> {110if( !button3.hasFocus() ) {111frame.dispose();112throw new RuntimeException(113"selected Button 3 should get focus");114}115});116117SwingUtilities.invokeLater(frame::dispose);118}119}120121122