1 /*
   2  * Copyright (c) 2015, 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  /*
  25  * @test
  26  * @library ../../regtesthelpers
  27  * @build Util
  28  * @bug 8075609
  29  * @summary  IllegalArgumentException when transferring focus from JRadioButton using tab
  30  * @author Vivi An
  31  * @run main bug8075609
  32  */
  33 
  34 import javax.swing.*;
  35 import javax.swing.event.*;
  36 import java.awt.event.*;
  37 import java.awt.*;
  38 
  39 public class bug8075609 {
  40     private static Robot robot;
  41     private static JTextField textField;
  42 
  43     public static void main(String args[]) throws Throwable {
  44         SwingUtilities.invokeAndWait(new Runnable() {
  45             public void run() {
  46                 createAndShowGUI();
  47             }
  48         });
  49 
  50         robot = new Robot();
  51         Thread.sleep(100);
  52 
  53         robot.setAutoDelay(100);
  54 
  55         // Radio button group tab key test
  56         runTest1();
  57     }
  58 
  59     private static void createAndShowGUI() {
  60         JFrame mainFrame = new JFrame("Bug 8075609 - 1 test");
  61 
  62         JPanel rootPanel = new JPanel();
  63         rootPanel.setLayout(new BorderLayout());
  64 
  65         JPanel formPanel = new JPanel();
  66         formPanel.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
  67         formPanel.setFocusCycleRoot(true);
  68 
  69         JRadioButton option1 = new JRadioButton("Option 1", true);
  70         JRadioButton option2 = new JRadioButton("Option 2");
  71 
  72         ButtonGroup radioButtonGroup = new ButtonGroup();
  73         radioButtonGroup.add(option1);
  74         radioButtonGroup.add(option2);
  75 
  76         formPanel.add(option1);
  77         formPanel.add(option2);
  78         textField = new JTextField("Another focusable component");
  79         formPanel.add(textField);
  80 
  81         rootPanel.add(formPanel, BorderLayout.CENTER);
  82 
  83         JButton okButton = new JButton("OK");
  84         rootPanel.add(okButton, BorderLayout.SOUTH);
  85 
  86         mainFrame.add(rootPanel);
  87         mainFrame.pack();
  88         mainFrame.setVisible(true);
  89         mainFrame.toFront();
  90     }
  91 
  92     // Radio button Group as a single component when traversing through tab key
  93     private static void runTest1() throws Exception{
  94         hitKey(robot, KeyEvent.VK_TAB);
  95 
  96         robot.delay(1000 );
  97         SwingUtilities.invokeAndWait(new Runnable() {
  98             public void run() {
  99                 if (!textField.hasFocus()) {
 100                     System.out.println("Radio Button Group Go To Next Component through Tab Key failed");
 101                     throw new RuntimeException("Focus is not on textField as Expected");
 102                 }
 103             }
 104         });
 105     }
 106 
 107     private static void hitKey(Robot robot, int keycode) {
 108         robot.keyPress(keycode);
 109         robot.keyRelease(keycode);
 110         robot.waitForIdle();
 111     }
 112 }