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 /* @test
  25    @bug 8129940
  26    @summary JRadioButton does not honor non-standard FocusTraversalKeys
  27    @author Semyon Sadetsky
  28   */
  29 
  30 import javax.swing.*;
  31 import java.awt.*;
  32 import java.awt.event.KeyEvent;
  33 import java.util.HashSet;
  34 import java.util.Set;
  35 
  36 public class FocusTraversal {
  37 
  38     private static JFrame frame;
  39     private static JRadioButton a;
  40     private static JRadioButton d;
  41     private static JTextField next;
  42     private static JTextField prev;
  43 
  44     public static void main(String[] args) throws Exception {
  45         SwingUtilities.invokeAndWait(new Runnable() {
  46             @Override
  47             public void run() {
  48                 frame = new JFrame("FocusTraversalTest");
  49                 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  50                 frame.setUndecorated(true);
  51 
  52                 Set<KeyStroke> keystrokes = new HashSet<KeyStroke>();
  53                 keystrokes.add(KeyStroke.getKeyStroke("TAB"));
  54                 keystrokes.add(KeyStroke.getKeyStroke("ENTER"));
  55                 frame.setFocusTraversalKeys(
  56                         KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  57                         keystrokes);
  58 
  59                 a = new JRadioButton("a");
  60                 JRadioButton b = new JRadioButton("b");
  61                 JRadioButton c = new JRadioButton("c");
  62                 d = new JRadioButton("d");
  63 
  64                 ButtonGroup radioButtonGroup = new ButtonGroup();
  65                 radioButtonGroup.add(a);
  66                 radioButtonGroup.add(b);
  67                 radioButtonGroup.add(c);
  68                 radioButtonGroup.add(d);
  69 
  70                 JPanel panel = new JPanel();
  71                 prev = new JTextField("text");
  72                 panel.add(prev);
  73                 panel.add(a);
  74                 panel.add(b);
  75                 panel.add(c);
  76                 panel.add(d);
  77                 next = new JTextField("text");
  78                 panel.add(next);
  79 
  80                 JPanel root = new JPanel();
  81                 root.setLayout(new BorderLayout());
  82                 root.add(panel, BorderLayout.CENTER);
  83                 root.add(new JButton("OK"), BorderLayout.SOUTH);
  84 
  85                 frame.add(root);
  86                 frame.pack();
  87                 frame.setVisible(true);
  88             }
  89         });
  90 
  91         SwingUtilities.invokeAndWait(new Runnable() {
  92             @Override
  93             public void run() {
  94                 a.requestFocus();
  95             }
  96         });
  97 
  98         Robot robot = new Robot();
  99         robot.waitForIdle();
 100 
 101         robot.setAutoDelay(200);
 102 
 103         robot.keyPress(KeyEvent.VK_ENTER);
 104         robot.keyRelease(KeyEvent.VK_ENTER);
 105         robot.waitForIdle();
 106 
 107         SwingUtilities.invokeAndWait(new Runnable() {
 108             @Override
 109             public void run() {
 110                 Component focusOwner =
 111                         FocusManager.getCurrentManager().getFocusOwner();
 112                 if (focusOwner != next) {
 113                     throw new RuntimeException(
 114                             "Focus component is wrong after forward key " + focusOwner);
 115                 }
 116             }
 117         });
 118 
 119         robot.keyPress(KeyEvent.VK_SHIFT);
 120         robot.keyPress(KeyEvent.VK_TAB);
 121         robot.keyRelease(KeyEvent.VK_TAB);
 122         robot.keyRelease(KeyEvent.VK_SHIFT);
 123         robot.waitForIdle();
 124         SwingUtilities.invokeAndWait(new Runnable() {
 125             @Override
 126             public void run() {
 127                 Component focusOwner =
 128                         FocusManager.getCurrentManager().getFocusOwner();
 129                 if (focusOwner != d) {
 130                     throw new RuntimeException(
 131                             "Focus component is wrong after backward key " + focusOwner);
 132                 }
 133             }
 134         });
 135         SwingUtilities.invokeLater(new Runnable() {
 136             @Override
 137             public void run() {
 138                 frame.dispose();
 139             }
 140         });
 141         System.out.println("ok");
 142 
 143     }
 144 }