Path: blob/master/test/jdk/java/awt/Focus/FocusTraversalPolicy/ButtonGroupLayoutTraversal/ButtonGroupLayoutTraversalTest.java
41154 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key headful26* @bug 8154043 817250927* @summary Fields not reachable anymore by tab-key, because of new tabbing28* behaviour of radio button groups.29* @run main ButtonGroupLayoutTraversalTest30*/3132import java.awt.BorderLayout;33import java.awt.Color;34import java.awt.GridLayout;35import java.awt.Robot;36import java.awt.event.FocusAdapter;37import java.awt.event.FocusEvent;38import java.awt.event.KeyEvent;39import javax.swing.JFrame;40import javax.swing.SwingUtilities;41import javax.swing.UIManager;42import javax.swing.JPanel;43import javax.swing.ButtonGroup;44import javax.swing.JComponent;45import javax.swing.JRadioButton;46import javax.swing.JToggleButton;47import javax.swing.LayoutFocusTraversalPolicy;48import javax.swing.UnsupportedLookAndFeelException;4950public class ButtonGroupLayoutTraversalTest {5152static int nx = 3;53static int ny = 3;5455static int focusCnt[] = new int[nx * ny];56private static JFrame window;5758public static void main(String[] args) throws Exception {5960try {61SwingUtilities.invokeAndWait(() -> changeLAF());62SwingUtilities.invokeAndWait(() -> initLayout(nx, ny));63Robot robot = new Robot();64robot.setAutoDelay(100);65robot.waitForIdle();66robot.delay(1000);6768for (int i = 0; i < nx * ny - nx * ny / 2 - 1; i++) {69robot.keyPress(KeyEvent.VK_RIGHT);70robot.keyRelease(KeyEvent.VK_RIGHT);71robot.waitForIdle();72}7374for (int i = 0; i < nx * ny / 2; i++) {75robot.keyPress(KeyEvent.VK_TAB);76robot.keyRelease(KeyEvent.VK_TAB);77robot.waitForIdle();78}7980robot.delay(200);8182for (int i = 0; i < nx * ny; i++) {83if (focusCnt[i] < 1) {84throw new RuntimeException("Component " + i85+ " is not reachable in the forward focus cycle");86} else if (focusCnt[i] > 1) {87throw new RuntimeException("Component " + i88+ " got focus more than once in the forward focus cycle");89}90}9192for (int i = 0; i < nx * ny / 2; i++) {93robot.keyPress(KeyEvent.VK_SHIFT);94robot.keyPress(KeyEvent.VK_TAB);95robot.keyRelease(KeyEvent.VK_TAB);96robot.keyRelease(KeyEvent.VK_SHIFT);97robot.waitForIdle();98}99100for (int i = 0; i < nx * ny - nx * ny / 2 - 1; i++) {101robot.keyPress(KeyEvent.VK_LEFT);102robot.keyRelease(KeyEvent.VK_LEFT);103robot.waitForIdle();104}105106robot.keyPress(KeyEvent.VK_SHIFT);107robot.keyPress(KeyEvent.VK_TAB);108robot.keyRelease(KeyEvent.VK_TAB);109robot.keyRelease(KeyEvent.VK_SHIFT);110robot.waitForIdle();111112robot.delay(200);113114for (int i = 0; i < nx * ny; i++) {115if (focusCnt[i] < 2) {116throw new RuntimeException("Component " + i117+ " is not reachable in the backward focus cycle");118} else if (focusCnt[i] > 2) {119throw new RuntimeException("Component " + i120+ " got focus more than once in the backward focus cycle");121}122}123} finally {124SwingUtilities.invokeAndWait(() -> {125if (window != null) {126window.dispose();127}128});129}130}131132private static void changeLAF() {133String currentLAF = UIManager.getLookAndFeel().toString();134currentLAF = currentLAF.toLowerCase();135if (currentLAF.contains("aqua") || currentLAF.contains("nimbus")) {136try {137UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");138} catch (ClassNotFoundException139| IllegalAccessException140| InstantiationException141| UnsupportedLookAndFeelException ex) {142ex.printStackTrace();143}144}145}146147public static void initLayout(int nx, int ny) {148window = new JFrame("Test");149window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);150JPanel rootPanel = new JPanel();151rootPanel.setLayout(new BorderLayout());152JPanel formPanel = new JPanel(new GridLayout(nx, ny));153formPanel.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());154formPanel.setFocusCycleRoot(true);155ButtonGroup radioButtonGroup = new ButtonGroup();156for (int i = 0; i < nx * ny; i++) {157JToggleButton comp;158if (i % 2 == 0) {159comp = new JRadioButton("Grouped component");160radioButtonGroup.add(comp);161} else {162comp = new JRadioButton("Single component");163}164formPanel.add(comp);165int fi = i;166comp.setBackground(Color.red);167comp.addFocusListener(new FocusAdapter() {168@Override169public void focusGained(FocusEvent e) {170focusCnt[fi]++;171if (focusCnt[fi] == 1) {172((JComponent) e.getSource())173.setBackground(Color.yellow);174} else if (focusCnt[fi] == 2) {175((JComponent) e.getSource())176.setBackground(Color.green);177} else {178((JComponent) e.getSource())179.setBackground(Color.red);180}181}182});183}184rootPanel.add(formPanel, BorderLayout.CENTER);185window.add(rootPanel);186window.setLocationRelativeTo(null);187window.pack();188window.setVisible(true);189}190}191192193