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