11/*
2- * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
3030 * @run main TestButtonGroupFocusTraversal
3131 */
3232
33- import javax .swing .AbstractAction ;
3433import javax .swing .ButtonGroup ;
3534import javax .swing .JCheckBox ;
3635import javax .swing .JFrame ;
4342import java .awt .Container ;
4443import java .awt .FlowLayout ;
4544import java .awt .KeyboardFocusManager ;
46- import java .awt .Point ;
4745import java .awt .Robot ;
48- import java .awt .event .ActionEvent ;
4946import java .awt .event .KeyEvent ;
5047
5148public class TestButtonGroupFocusTraversal {
5249 private static JFrame frame ;
5350 private static JTextField textFieldFirst , textFieldLast ;
5451 private static JToggleButton toggleButton1 , toggleButton2 ;
5552 private static JCheckBox checkBox1 , checkBox2 ;
56- private static boolean toggleButtonActionPerformed ;
57- private static boolean checkboxActionPerformed ;
53+ private static volatile boolean toggleButtonActionPerformed ;
54+ private static volatile boolean checkboxActionPerformed ;
5855 private static JRadioButton radioButton1 , radioButton2 ;
5956 private static Robot robot ;
6057
61- private static void blockTillDisplayed (Component comp ) {
62- Point p = null ;
63- while (p == null ) {
64- try {
65- p = comp .getLocationOnScreen ();
66- } catch (IllegalStateException e ) {
67- try {
68- Thread .sleep (500 );
69- } catch (InterruptedException ie ) {
70- }
71- }
72- }
73- }
74-
7558 private static void createUI () throws Exception {
7659 SwingUtilities .invokeAndWait (new Runnable () {
7760 public void run () {
@@ -84,33 +67,11 @@ public void run() {
8467 checkBox1 = new JCheckBox ("1" );
8568 checkBox2 = new JCheckBox ("2" );
8669
87- toggleButton1 .setAction (new AbstractAction () {
88- @ Override
89- public void actionPerformed (ActionEvent e ) {
90- toggleButtonActionPerformed = true ;
91- }
92- });
93-
94- toggleButton2 .setAction (new AbstractAction () {
95- @ Override
96- public void actionPerformed (ActionEvent e ) {
97- toggleButtonActionPerformed = true ;
98- }
99- });
100-
101- checkBox1 .setAction (new AbstractAction () {
102- @ Override
103- public void actionPerformed (ActionEvent e ) {
104- checkboxActionPerformed = true ;
105- }
106- });
107-
108- checkBox2 .setAction (new AbstractAction () {
109- @ Override
110- public void actionPerformed (ActionEvent e ) {
111- checkboxActionPerformed = true ;
112- }
113- });
70+ toggleButton1 .addActionListener ((_ ) -> toggleButtonActionPerformed = true );
71+ toggleButton2 .addActionListener ((_ ) -> toggleButtonActionPerformed = true );
72+
73+ checkBox1 .addActionListener ((_ ) -> checkboxActionPerformed = true );
74+ checkBox2 .addActionListener ((_ ) -> checkboxActionPerformed = true );
11475
11576 ButtonGroup toggleGroup = new ButtonGroup ();
11677 toggleGroup .add (toggleButton1 );
@@ -128,7 +89,7 @@ public void actionPerformed(ActionEvent e) {
12889 radioButton2 .setSelected (true );
12990 checkBox2 .setSelected (true );
13091
131- frame = new JFrame ("Test " );
92+ frame = new JFrame ("TestButtonGroupFocusTraversal " );
13293 frame .setLayout (new FlowLayout ());
13394
13495 Container pane = frame .getContentPane ();
@@ -178,7 +139,7 @@ private static void checkToggleButtonActionPerformed() {
178139 }
179140
180141 private static void checkCheckboxActionPerformed () {
181- if (toggleButtonActionPerformed ) {
142+ if (checkboxActionPerformed ) {
182143 throw new RuntimeException ("Checkbox Action should not be" +
183144 "performed" );
184145 }
@@ -196,19 +157,13 @@ public static void main(String[] args) throws Exception {
196157 createUI ();
197158
198159 robot .waitForIdle ();
199- robot .delay (200 );
200-
201- blockTillDisplayed (frame );
160+ robot .delay (500 );
202161
203162 SwingUtilities .invokeAndWait (textFieldFirst ::requestFocus );
204163
205164 if (!textFieldFirst .equals (KeyboardFocusManager .getCurrentKeyboardFocusManager ()
206165 .getFocusOwner ())) {
207- try {
208- Thread .sleep (100 );
209- } catch (InterruptedException e ) {
210- e .printStackTrace ();
211- }
166+ robot .delay (300 );
212167 SwingUtilities .invokeAndWait (textFieldFirst ::requestFocus );
213168 }
214169
0 commit comments