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