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