11/*
2- * Copyright (c) 2004, 2014 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2004, 2022 , 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
2828 * @key headful
2929 * @bug 4529206
3030 * @summary JToolBar - setFloating does not work correctly
31- * @author Konstantin Eremin
3231 * @run main bug4529206
3332 */
3433
35- import javax .swing .*;
36- import java .awt .*;
34+ import java .awt .BorderLayout ;
35+ import java .awt .Dimension ;
36+ import java .awt .Robot ;
3737import java .awt .event .ActionEvent ;
3838import java .awt .event .ActionListener ;
39+ import javax .swing .JButton ;
40+ import javax .swing .JFrame ;
41+ import javax .swing .JPanel ;
42+ import javax .swing .JTextField ;
43+ import javax .swing .JToolBar ;
44+ import javax .swing .SwingUtilities ;
3945
40- public class bug4529206 extends JFrame {
46+ public class bug4529206 {
4147 static JFrame frame ;
4248 static JToolBar jToolBar1 ;
43- public bug4529206 () {
44- setDefaultCloseOperation (EXIT_ON_CLOSE );
45- JPanel jPanFrame = (JPanel ) this .getContentPane ();
49+ static JButton jButton1 ;
50+
51+ private static void test () {
52+ frame = new JFrame ();
53+ JPanel jPanFrame = (JPanel ) frame .getContentPane ();
4654 jPanFrame .setLayout (new BorderLayout ());
47- this .setSize (new Dimension (200 , 100 ));
48- this .setLocation (125 , 75 );
49- this .setTitle ("Test Floating Toolbar" );
55+ frame .setSize (new Dimension (200 , 100 ));
56+ frame .setTitle ("Test Floating Toolbar" );
5057 jToolBar1 = new JToolBar ();
51- JButton jButton1 = new JButton ("Float" );
58+ jButton1 = new JButton ("Float" );
5259 jPanFrame .add (jToolBar1 , BorderLayout .NORTH );
5360 JTextField tf = new JTextField ("click here" );
5461 jPanFrame .add (tf );
@@ -58,36 +65,54 @@ public void actionPerformed(ActionEvent e) {
5865 buttonPressed (e );
5966 }
6067 });
61- makeToolbarFloat ();
62- setVisible (true );
68+
69+ frame .setUndecorated (true );
70+ frame .setLocationRelativeTo (null );
71+ frame .setVisible (true );
6372 }
6473
65- private void makeToolbarFloat () {
74+ private static void makeToolbarFloat () {
6675 javax .swing .plaf .basic .BasicToolBarUI ui = (javax .swing .plaf .basic .BasicToolBarUI ) jToolBar1 .getUI ();
6776 if (!ui .isFloating ()) {
6877 ui .setFloatingLocation (100 , 100 );
6978 ui .setFloating (true , jToolBar1 .getLocation ());
7079 }
7180 }
7281
73- private void buttonPressed (ActionEvent e ) {
82+ private static void buttonPressed (ActionEvent e ) {
7483 makeToolbarFloat ();
7584 }
7685
7786 public static void main (String [] args ) throws Exception {
78- SwingUtilities .invokeAndWait (new Runnable () {
79- public void run () {
80- frame = new bug4529206 ();
81- }
82- });
83- Robot robot = new Robot ();
84- robot .waitForIdle ();
85- SwingUtilities .invokeAndWait (new Runnable () {
86- public void run () {
87- if (frame .isFocused ()) {
88- throw (new RuntimeException ("setFloating does not work correctly" ));
87+ try {
88+ SwingUtilities .invokeAndWait (new Runnable () {
89+ public void run () {
90+ test ();
8991 }
90- }
91- });
92+ });
93+ Robot robot = new Robot ();
94+ robot .waitForIdle ();
95+ robot .delay (1000 );
96+
97+ SwingUtilities .invokeAndWait (() -> {
98+ makeToolbarFloat ();
99+ });
100+
101+ robot .waitForIdle ();
102+ SwingUtilities .invokeAndWait (new Runnable () {
103+ public void run () {
104+ if (frame .isFocused ()) {
105+ throw
106+ new RuntimeException ("setFloating does not work correctly" );
107+ }
108+ }
109+ });
110+ } finally {
111+ SwingUtilities .invokeAndWait (() -> {
112+ if (frame != null ) {
113+ frame .dispose ();
114+ }
115+ });
116+ }
92117 }
93118}
0 commit comments