11/*
2- * Copyright (c) 2002, 2024 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2002, 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*/
3131
3232import java .awt .Button ;
33+ import java .awt .EventQueue ;
3334import java .awt .FlowLayout ;
3435import java .awt .Frame ;
3536import java .awt .KeyboardFocusManager ;
@@ -48,19 +49,27 @@ public class InactiveFocusRace {
4849 Button activeButton , inactiveButton1 , inactiveButton2 ;
4950 Semaphore sema ;
5051 final static int TIMEOUT = 10000 ;
52+ private static Robot robot ;
5153
5254 public static void main (String [] args ) throws Exception {
5355 try {
56+ robot = new Robot ();
5457 InactiveFocusRace test = new InactiveFocusRace ();
55- test .init ();
58+ EventQueue .invokeAndWait (() -> {
59+ test .init ();
60+ });
61+ robot .waitForIdle ();
62+ robot .delay (1000 );
5663 test .start ();
5764 } finally {
58- if (activeFrame != null ) {
59- activeFrame .dispose ();
60- }
61- if (inactiveFrame != null ) {
62- inactiveFrame .dispose ();
63- }
65+ EventQueue .invokeAndWait (() -> {
66+ if (activeFrame != null ) {
67+ activeFrame .dispose ();
68+ }
69+ if (inactiveFrame != null ) {
70+ inactiveFrame .dispose ();
71+ }
72+ });
6473 }
6574 }
6675
@@ -91,24 +100,40 @@ public void focusGained(FocusEvent e) {
91100 sema .raise ();
92101 }
93102 });
103+ inactiveFrame .addWindowListener (new WindowAdapter () {
104+ public void windowActivated (WindowEvent e ) {
105+ System .err .println ("inactive Window activated" );
106+ sema .raise ();
107+ }
108+ });
94109 activeFrame .addWindowListener (new WindowAdapter () {
95110 public void windowActivated (WindowEvent e ) {
96111 System .err .println ("Window activated" );
97112 sema .raise ();
98113 }
99114 });
115+ inactiveFrame .setVisible (true );
100116 }
101117
102118 public void start () {
103- Robot robot = null ;
119+
120+
121+ // Wait for inactive frame to become active
104122 try {
105- robot = new Robot ();
106- } catch (Exception e ) {
107- throw new RuntimeException ("Unable to create robot" );
123+ sema .doWait (TIMEOUT );
124+ } catch (InterruptedException ie ) {
125+ throw new RuntimeException ("Wait was interrupted" );
126+ }
127+ if (!sema .getState ()) {
128+ throw new RuntimeException ("Frame doesn't become active on show" );
108129 }
130+ sema .setState (false );
109131
110- inactiveFrame .setVisible (true );
111- activeFrame .setVisible (true );
132+ try {
133+ EventQueue .invokeAndWait (() -> activeFrame .setVisible (true ));
134+ } catch (Exception e ) {
135+ throw new RuntimeException ("Interrupted active frame rendering" );
136+ }
112137
113138 // Wait for active frame to become active
114139 try {
0 commit comments