11/*
2- * Copyright (c) 2004, 2024 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2004, 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
3535import java .awt .Frame ;
3636import java .awt .KeyboardFocusManager ;
3737import java .awt .Point ;
38+ import java .awt .Rectangle ;
3839import java .awt .Robot ;
3940import java .awt .TextField ;
41+ import java .awt .Toolkit ;
4042import java .awt .event .FocusAdapter ;
4143import java .awt .event .FocusEvent ;
4244import java .awt .event .InputEvent ;
4345import java .awt .event .WindowAdapter ;
4446import java .awt .event .WindowEvent ;
47+ import java .io .File ;
48+ import java .util .concurrent .CountDownLatch ;
49+ import java .util .concurrent .TimeUnit ;
50+
51+ import javax .imageio .ImageIO ;
4552
4653public class ComponentLostFocusTest {
4754
4855 static Frame frame ;
4956 static TextField tf ;
5057 static Robot r ;
5158 static Dialog dialog = null ;
52- static volatile boolean passed ;
5359 static volatile Point loc ;
5460 static volatile int width ;
5561 static volatile int top ;
62+ static final CountDownLatch focusGainedLatch = new CountDownLatch (1 );
5663
5764 private static void createTestUI () {
5865
@@ -75,11 +82,7 @@ public void windowGainedFocus(WindowEvent e) {
7582
7683 public static void doTest () {
7784 System .out .println ("dialog.setVisible.... " );
78- new Thread (new Runnable () {
79- public void run () {
80- dialog .setVisible (true );
81- }
82- }).start ();
85+ new Thread (() -> dialog .setVisible (true )).start ();
8386
8487 // The bug is that this construction leads to the redundant xRequestFocus
8588 // By the way, the requestFocusInWindow() works fine before the fix
@@ -98,7 +101,7 @@ public void run() {
98101 tf .addFocusListener (new FocusAdapter () {
99102 public void focusGained (FocusEvent e ) {
100103 System .out .println ("TextField gained focus: " + e );
101- passed = true ;
104+ focusGainedLatch . countDown () ;
102105 }
103106 });
104107
@@ -116,6 +119,17 @@ private static void doRequestFocusToTextField() {
116119 tf .requestFocus ();
117120 }
118121
122+ private static void captureScreen () {
123+ try {
124+ final Rectangle screenBounds = new Rectangle (
125+ Toolkit .getDefaultToolkit ().getScreenSize ());
126+ ImageIO .write (r .createScreenCapture (screenBounds ),
127+ "png" , new File ("ComponentLostFocusTest.png" ));
128+ } catch (Exception e ) {
129+ e .printStackTrace ();
130+ }
131+ }
132+
119133 public static final void main (String args []) throws Exception {
120134 r = new Robot ();
121135 r .setAutoDelay (100 );
@@ -138,9 +152,10 @@ public static final void main(String args[]) throws Exception {
138152 System .out .println ("Focus owner: " +
139153 KeyboardFocusManager .getCurrentKeyboardFocusManager ().
140154 getFocusOwner ());
141-
142- if (!passed ) {
143- throw new RuntimeException ("TextField got no focus! Test failed." );
155+ if (!focusGainedLatch .await (5 , TimeUnit .SECONDS )) {
156+ captureScreen ();
157+ throw new RuntimeException ("Waited too long, " +
158+ "TextField got no focus! Test failed." );
144159 }
145160 } finally {
146161 EventQueue .invokeAndWait (() -> {
@@ -151,4 +166,3 @@ public static final void main(String args[]) throws Exception {
151166 }
152167 }
153168}
154-
0 commit comments