1 /* 2 * Copyright (c) 2016, 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 @bug 8154043 8172509 27 @summary Fields not reachable anymore by tab-key, because of new tabbing 28 behaviour of radio button groups. 29 @run main ButtonGroupLayoutTraversalTest 30 */ 31 import java.awt.BorderLayout; 32 import java.awt.Color; 33 import java.awt.GridLayout; 34 import java.awt.Robot; 35 import java.awt.event.FocusAdapter; 36 import java.awt.event.FocusEvent; 37 import java.awt.event.KeyEvent; 38 import javax.swing.JFrame; 39 import javax.swing.SwingUtilities; 40 import javax.swing.UIManager; 41 import javax.swing.JPanel; 42 import javax.swing.ButtonGroup; 43 import javax.swing.JComponent; 44 import javax.swing.JRadioButton; 45 import javax.swing.JToggleButton; 46 import javax.swing.LayoutFocusTraversalPolicy; 47 import javax.swing.UnsupportedLookAndFeelException; 48 49 public class ButtonGroupLayoutTraversalTest { 50 51 static int nx = 3; 52 static int ny = 3; 53 54 static int focusCnt[] = new int[nx * ny]; 55 private static JFrame window; 56 57 public static void main(String[] args) throws Exception { 58 59 SwingUtilities.invokeAndWait(() -> changeLAF()); 60 SwingUtilities.invokeAndWait(() -> initLayout(nx, ny)); 61 Robot robot = new Robot(); 62 robot.setAutoDelay(100); 63 robot.waitForIdle(); 64 robot.delay(200); 65 66 for (int i = 0; i < nx * ny - nx * ny / 2 - 1; i++) { 67 robot.keyPress(KeyEvent.VK_RIGHT); 68 robot.keyRelease(KeyEvent.VK_RIGHT); 69 } 70 71 for (int i = 0; i < nx * ny / 2; i++) { 72 robot.keyPress(KeyEvent.VK_TAB); 73 robot.keyRelease(KeyEvent.VK_TAB); 74 } 75 76 robot.waitForIdle(); 77 robot.delay(200); 78 79 for (int i = 0; i < nx * ny; i++) { 80 if (focusCnt[i] < 1) { 81 SwingUtilities.invokeLater(window::dispose); 82 throw new RuntimeException("Component " + i 83 + " is not reachable in the forward focus cycle"); 84 } else if (focusCnt[i] > 1) { 85 SwingUtilities.invokeLater(window::dispose); 86 throw new RuntimeException("Component " + i 87 + " got focus more than once in the forward focus cycle"); 88 } 89 } 90 91 for (int i = 0; i < nx * ny / 2; i++) { 92 robot.keyPress(KeyEvent.VK_SHIFT); 93 robot.keyPress(KeyEvent.VK_TAB); 94 robot.keyRelease(KeyEvent.VK_TAB); 95 robot.keyRelease(KeyEvent.VK_SHIFT); 96 } 97 98 for (int i = 0; i < nx * ny - nx * ny / 2 - 1; i++) { 99 robot.keyPress(KeyEvent.VK_LEFT); 100 robot.keyRelease(KeyEvent.VK_LEFT); 101 } 102 103 robot.keyPress(KeyEvent.VK_SHIFT); 104 robot.keyPress(KeyEvent.VK_TAB); 105 robot.keyRelease(KeyEvent.VK_TAB); 106 robot.keyRelease(KeyEvent.VK_SHIFT); 107 108 robot.waitForIdle(); 109 robot.delay(200); 110 111 for (int i = 0; i < nx * ny; i++) { 112 if (focusCnt[i] < 2) { 113 SwingUtilities.invokeLater(window::dispose); 114 throw new RuntimeException("Component " + i 115 + " is not reachable in the backward focus cycle"); 116 } else if (focusCnt[i] > 2) { 117 SwingUtilities.invokeLater(window::dispose); 118 throw new RuntimeException("Component " + i 119 + " got focus more than once in the backward focus cycle"); 120 } 121 } 122 123 SwingUtilities.invokeLater(window::dispose); 124 } 125 126 private static void changeLAF() { 127 String currentLAF = UIManager.getLookAndFeel().toString(); 128 currentLAF = currentLAF.toLowerCase(); 129 if (currentLAF.contains("aqua") || currentLAF.contains("nimbus")) { 130 try { 131 UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 132 } catch (ClassNotFoundException 133 | IllegalAccessException 134 | InstantiationException 135 | UnsupportedLookAndFeelException ex) { 136 ex.printStackTrace(); 137 } 138 } 139 } 140 141 public static void initLayout(int nx, int ny) { 142 window = new JFrame("Test"); 143 window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 144 JPanel rootPanel = new JPanel(); 145 rootPanel.setLayout(new BorderLayout()); 146 JPanel formPanel = new JPanel(new GridLayout(nx, ny)); 147 formPanel.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy()); 148 formPanel.setFocusCycleRoot(true); 149 ButtonGroup radioButtonGroup = new ButtonGroup(); 150 for (int i = 0; i < nx * ny; i++) { 151 JToggleButton comp; 152 if (i % 2 == 0) { 153 comp = new JRadioButton("Grouped component"); 154 radioButtonGroup.add(comp); 155 } else { 156 comp = new JRadioButton("Single component"); 157 } 158 formPanel.add(comp); 159 int fi = i; 160 comp.setBackground(Color.red); 161 comp.addFocusListener(new FocusAdapter() { 162 @Override 163 public void focusGained(FocusEvent e) { 164 focusCnt[fi]++; 165 if (focusCnt[fi] == 1) { 166 ((JComponent) e.getSource()) 167 .setBackground(Color.yellow); 168 } else if (focusCnt[fi] == 2) { 169 ((JComponent) e.getSource()) 170 .setBackground(Color.green); 171 } else { 172 ((JComponent) e.getSource()) 173 .setBackground(Color.red); 174 } 175 } 176 }); 177 } 178 rootPanel.add(formPanel, BorderLayout.CENTER); 179 window.add(rootPanel); 180 window.pack(); 181 window.setVisible(true); 182 } 183 }