1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
28
28
* @key headful
29
29
* @bug 4529206
30
30
* @summary JToolBar - setFloating does not work correctly
31
- * @author Konstantin Eremin
32
31
* @run main bug4529206
33
32
*/
34
33
35
- import javax .swing .*;
36
- import java .awt .*;
34
+ import java .awt .BorderLayout ;
35
+ import java .awt .Dimension ;
36
+ import java .awt .Robot ;
37
37
import java .awt .event .ActionEvent ;
38
38
import 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 ;
39
45
40
- public class bug4529206 extends JFrame {
46
+ public class bug4529206 {
41
47
static JFrame frame ;
42
48
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 ();
46
54
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" );
50
57
jToolBar1 = new JToolBar ();
51
- JButton jButton1 = new JButton ("Float" );
58
+ jButton1 = new JButton ("Float" );
52
59
jPanFrame .add (jToolBar1 , BorderLayout .NORTH );
53
60
JTextField tf = new JTextField ("click here" );
54
61
jPanFrame .add (tf );
@@ -58,36 +65,54 @@ public void actionPerformed(ActionEvent e) {
58
65
buttonPressed (e );
59
66
}
60
67
});
61
- makeToolbarFloat ();
62
- setVisible (true );
68
+
69
+ frame .setUndecorated (true );
70
+ frame .setLocationRelativeTo (null );
71
+ frame .setVisible (true );
63
72
}
64
73
65
- private void makeToolbarFloat () {
74
+ private static void makeToolbarFloat () {
66
75
javax .swing .plaf .basic .BasicToolBarUI ui = (javax .swing .plaf .basic .BasicToolBarUI ) jToolBar1 .getUI ();
67
76
if (!ui .isFloating ()) {
68
77
ui .setFloatingLocation (100 , 100 );
69
78
ui .setFloating (true , jToolBar1 .getLocation ());
70
79
}
71
80
}
72
81
73
- private void buttonPressed (ActionEvent e ) {
82
+ private static void buttonPressed (ActionEvent e ) {
74
83
makeToolbarFloat ();
75
84
}
76
85
77
86
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 ();
89
91
}
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
+ }
92
117
}
93
118
}
0 commit comments