Path: blob/master/test/jdk/javax/swing/JRadioButton/8075609/bug8075609.java
41153 views
/*1* Copyright (c) 2015, 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/*24* @test25* @key headful26* @library ../../regtesthelpers27* @build Util28* @bug 807560929* @summary IllegalArgumentException when transferring focus from JRadioButton using tab30* @author Vivi An31* @run main bug807560932*/3334import javax.swing.*;35import javax.swing.event.*;36import java.awt.event.*;37import java.awt.*;3839public class bug8075609 {40private static Robot robot;41private static JTextField textField;42private static JFrame mainFrame;4344public static void main(String args[]) throws Throwable {45try {46SwingUtilities.invokeAndWait(new Runnable() {47public void run() {48createAndShowGUI();49}50});5152robot = new Robot();53Thread.sleep(100);5455robot.setAutoDelay(100);5657// Radio button group tab key test58runTest1();59} finally {60if (mainFrame != null) SwingUtilities.invokeAndWait(() -> mainFrame.dispose());61}62}6364private static void createAndShowGUI() {65mainFrame = new JFrame("Bug 8075609 - 1 test");6667JPanel rootPanel = new JPanel();68rootPanel.setLayout(new BorderLayout());6970JPanel formPanel = new JPanel();71formPanel.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());72formPanel.setFocusCycleRoot(true);7374JRadioButton option1 = new JRadioButton("Option 1", true);75JRadioButton option2 = new JRadioButton("Option 2");7677ButtonGroup radioButtonGroup = new ButtonGroup();78radioButtonGroup.add(option1);79radioButtonGroup.add(option2);8081formPanel.add(option1);82formPanel.add(option2);83textField = new JTextField("Another focusable component");84formPanel.add(textField);8586rootPanel.add(formPanel, BorderLayout.CENTER);8788JButton okButton = new JButton("OK");89rootPanel.add(okButton, BorderLayout.SOUTH);9091mainFrame.add(rootPanel);92mainFrame.pack();93mainFrame.setVisible(true);94mainFrame.toFront();95}9697// Radio button Group as a single component when traversing through tab key98private static void runTest1() throws Exception{99hitKey(robot, KeyEvent.VK_TAB);100101robot.delay(1000 );102SwingUtilities.invokeAndWait(new Runnable() {103public void run() {104if (!textField.hasFocus()) {105System.out.println("Radio Button Group Go To Next Component through Tab Key failed");106throw new RuntimeException("Focus is not on textField as Expected");107}108}109});110}111112private static void hitKey(Robot robot, int keycode) {113robot.keyPress(keycode);114robot.keyRelease(keycode);115robot.waitForIdle();116}117}118119120