1
1
/*
2
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2017, 2021, 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
46
46
47
47
public class ContextMenuScrollTest extends JPopupMenu
48
48
{
49
- private JMenuItem undo ;
50
- private JMenuItem redo ;
51
- private JMenuItem cut ;
52
- private JMenuItem copy ;
53
- private JMenuItem paste ;
54
- private JMenuItem delete ;
55
- private JMenuItem selectAll ;
56
- private final Robot robot ;
57
- private JFrame frame ;
58
- private JMenuBar menuBar ;
59
- private JMenu menu ;
60
- private volatile Point p = null ;
61
- private volatile Dimension d = null ;
49
+ private static Robot robot ;
50
+ private static JFrame frame ;
51
+ private static JMenu menu ;
52
+ private static volatile Point p = null ;
53
+ private static volatile Dimension d = null ;
54
+ private static volatile boolean popupVisible = false ;
62
55
63
56
public static void main (String [] args ) throws Exception {
64
- new ContextMenuScrollTest ();
65
- }
66
- void blockTillDisplayed (JComponent comp ) throws Exception {
67
- while (p == null ) {
68
- try {
69
- SwingUtilities .invokeAndWait (() -> {
70
- p = comp .getLocationOnScreen ();
71
- d = menu .getSize ();
72
- });
73
- } catch (IllegalStateException e ) {
74
- try {
75
- Thread .sleep (1000 );
76
- } catch (InterruptedException ie ) {
77
- }
78
- }
79
- }
80
- }
81
-
82
- public ContextMenuScrollTest () throws Exception
83
- {
84
57
robot = new Robot ();
85
- robot .setAutoDelay (200 );
58
+ robot .setAutoDelay (100 );
86
59
try {
87
60
SwingUtilities .invokeAndWait (()->createGUI ());
88
- blockTillDisplayed (menu );
89
61
robot .waitForIdle ();
62
+ robot .delay (1000 );
90
63
64
+ SwingUtilities .invokeAndWait (() -> {
65
+ p = menu .getLocationOnScreen ();
66
+ d = menu .getSize ();
67
+ });
68
+ System .out .println ("p " + p + " d " + d );
91
69
robot .mouseMove (p .x + d .width /2 , p .y + d .height /2 );
92
- robot .mousePress (InputEvent .BUTTON1_MASK );
93
- robot .mouseRelease (InputEvent .BUTTON1_MASK );
70
+ robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
71
+ robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
94
72
robot .waitForIdle ();
73
+ robot .delay (1000 );
95
74
96
- System .out .println ("popmenu visible " + menu .isPopupMenuVisible ());
97
75
robot .mouseWheel (1 );
98
76
robot .waitForIdle ();
99
- System .out .println ("popmenu visible " + menu .isPopupMenuVisible ());
100
- if (!menu .isPopupMenuVisible ()) {
77
+
78
+ SwingUtilities .invokeAndWait (() -> {
79
+ popupVisible = menu .isPopupMenuVisible ();
80
+ });
81
+ if (!popupVisible ) {
101
82
throw new RuntimeException ("Popup closes on mouse scroll" );
102
83
}
103
84
} finally {
104
85
SwingUtilities .invokeAndWait (()->frame .dispose ());
105
86
}
106
87
}
107
88
108
- public void createGUI () {
89
+
90
+ public static void createGUI () {
109
91
frame = new JFrame ();
110
- menuBar = new JMenuBar ();
92
+ JMenuBar menuBar = new JMenuBar ();
111
93
menu = new JMenu ("Menu" );
112
94
menuBar .add (menu );
113
95
114
- undo = new JMenuItem ("Undo" );
96
+ JMenuItem undo = new JMenuItem ("Undo" );
115
97
undo .setEnabled (false );
116
98
undo .setAccelerator (KeyStroke .getKeyStroke ("control Z" ));
117
99
undo .addActionListener (new ActionListener () {
@@ -122,7 +104,7 @@ public void actionPerformed(ActionEvent event) {
122
104
123
105
menu .add (undo );
124
106
125
- redo = new JMenuItem ("Redo" );
107
+ JMenuItem redo = new JMenuItem ("Redo" );
126
108
redo .setEnabled (false );
127
109
redo .setAccelerator (KeyStroke .getKeyStroke ("control Y" ));
128
110
redo .addActionListener (new ActionListener () {
@@ -134,7 +116,7 @@ public void actionPerformed(ActionEvent event) {
134
116
135
117
menu .add (new JSeparator ());
136
118
137
- cut = new JMenuItem ("Cut" );
119
+ JMenuItem cut = new JMenuItem ("Cut" );
138
120
cut .setEnabled (false );
139
121
cut .setAccelerator (KeyStroke .getKeyStroke ("control X" ));
140
122
cut .addActionListener (new ActionListener () {
@@ -145,7 +127,7 @@ public void actionPerformed(ActionEvent event) {
145
127
146
128
menu .add (cut );
147
129
148
- copy = new JMenuItem ("Copy" );
130
+ JMenuItem copy = new JMenuItem ("Copy" );
149
131
copy .setEnabled (false );
150
132
copy .setAccelerator (KeyStroke .getKeyStroke ("control C" ));
151
133
copy .addActionListener (new ActionListener () {
@@ -156,7 +138,7 @@ public void actionPerformed(ActionEvent event) {
156
138
157
139
menu .add (copy );
158
140
159
- paste = new JMenuItem ("Paste" );
141
+ JMenuItem paste = new JMenuItem ("Paste" );
160
142
paste .setEnabled (false );
161
143
paste .setAccelerator (KeyStroke .getKeyStroke ("control V" ));
162
144
paste .addActionListener (new ActionListener () {
@@ -167,7 +149,7 @@ public void actionPerformed(ActionEvent event) {
167
149
168
150
menu .add (paste );
169
151
170
- delete = new JMenuItem ("Delete" );
152
+ JMenuItem delete = new JMenuItem ("Delete" );
171
153
delete .setEnabled (false );
172
154
delete .setAccelerator (KeyStroke .getKeyStroke (KeyEvent .VK_DELETE , 0 ));
173
155
delete .addActionListener (new ActionListener () {
@@ -180,7 +162,7 @@ public void actionPerformed(ActionEvent event) {
180
162
181
163
menu .add (new JSeparator ());
182
164
183
- selectAll = new JMenuItem ("Select All" );
165
+ JMenuItem selectAll = new JMenuItem ("Select All" );
184
166
selectAll .setEnabled (false );
185
167
selectAll .setAccelerator (KeyStroke .getKeyStroke ("control A" ));
186
168
selectAll .addActionListener (new ActionListener () {
@@ -192,6 +174,7 @@ public void actionPerformed(ActionEvent event) {
192
174
frame .setJMenuBar (menuBar );
193
175
194
176
frame .pack ();
177
+ frame .setLocationRelativeTo (null );
195
178
frame .setVisible (true );
196
179
}
197
180
}
0 commit comments