Skip to content

Commit 65ca5c6

Browse files
committed
8048109: JToggleButton does not fire actionPerformed under certain conditions
Reviewed-by: serb, psadhukhan, vdyakov
1 parent 81db63e commit 65ca5c6

File tree

5 files changed

+169
-4
lines changed

5 files changed

+169
-4
lines changed

src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKLookAndFeel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ public Object createValue(UIDefaults table) {
803803
"PasswordField.font", new FontLazyValue(Region.PASSWORD_FIELD),
804804

805805

806-
"PopupMenu.consumeEventOnClose", Boolean.TRUE,
806+
"PopupMenu.consumeEventOnClose", Boolean.FALSE,
807807
"PopupMenu.selectedWindowInputMapBindings", new Object[] {
808808
"ESCAPE", "cancel",
809809
"DOWN", "selectNext",

src/java.desktop/share/classes/com/sun/java/swing/plaf/motif/MotifLookAndFeel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ public Object createValue(UIDefaults table) {
678678
"PopupMenu.border", popupMenuBorder,
679679
"PopupMenu.foreground", table.get("menuText"),
680680
"PopupMenu.font", dialogPlain12,
681-
"PopupMenu.consumeEventOnClose", Boolean.TRUE,
681+
"PopupMenu.consumeEventOnClose", Boolean.FALSE,
682682

683683
"Label.font", dialogPlain12,
684684
"Label.background", table.get("control"),

src/java.desktop/share/classes/javax/swing/plaf/nimbus/skin.laf

+1-1
Original file line numberDiff line numberDiff line change
@@ -15193,7 +15193,7 @@
1519315193
<maxHozCachedImgScaling>INF</maxHozCachedImgScaling>
1519415194
<maxVertCachedImgScaling>INF</maxVertCachedImgScaling>
1519515195
<uiproperties>
15196-
<uiProperty name="consumeEventOnClose" type="BOOLEAN" value="true"/>
15196+
<uiProperty name="consumeEventOnClose" type="BOOLEAN" value="false"/>
1519715197
</uiproperties>
1519815198
</style>
1519915199
<backgroundStates>

src/java.desktop/windows/classes/com/sun/java/swing/plaf/windows/WindowsLookAndFeel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ protected void initComponentDefaults(UIDefaults table)
958958
"PopupMenu.background", MenuBackgroundColor,
959959
"PopupMenu.foreground", MenuTextColor,
960960
"PopupMenu.popupSound", "win.sound.menuPopup",
961-
"PopupMenu.consumeEventOnClose", Boolean.TRUE,
961+
"PopupMenu.consumeEventOnClose", Boolean.FALSE,
962962

963963
// Menus
964964
"Menu.font", MenuFont,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import javax.swing.JButton;
25+
import javax.swing.JFrame;
26+
import javax.swing.JMenuItem;
27+
import javax.swing.JPopupMenu;
28+
import javax.swing.SwingUtilities;
29+
import javax.swing.UIManager;
30+
import javax.swing.UnsupportedLookAndFeelException;
31+
import java.awt.Container;
32+
import java.awt.FlowLayout;
33+
import java.awt.Point;
34+
import java.awt.Robot;
35+
import java.awt.event.ActionEvent;
36+
import java.awt.event.ActionListener;
37+
import java.awt.event.InputEvent;
38+
import java.awt.event.MouseEvent;
39+
import java.io.StringWriter;
40+
import java.io.PrintWriter;
41+
42+
/**
43+
* @test
44+
* @bug 8048109
45+
* @summary JToggleButton does not fire actionPerformed under certain
46+
* conditions
47+
* @key headful
48+
* @run main SetInvokerJPopupMenuTest
49+
*/
50+
51+
public class SetInvokerJPopupMenuTest {
52+
53+
private static MyPopupMenu popup;
54+
private static MyButton jtb ;
55+
private static Robot robot;
56+
private static JFrame f;
57+
private static Point p;
58+
private static boolean isPopupVisible;
59+
60+
public static void main(String[] args) throws Exception {
61+
UIManager.LookAndFeelInfo[] installedLookAndFeels;
62+
installedLookAndFeels = UIManager.getInstalledLookAndFeels();
63+
64+
for(UIManager.LookAndFeelInfo LF : installedLookAndFeels) {
65+
try {
66+
robot = new Robot();
67+
UIManager.setLookAndFeel(LF.getClassName());
68+
SwingUtilities.invokeAndWait(() -> {
69+
jtb = new MyButton("Press Me");
70+
jtb.addActionListener(new ActionListener( ) {
71+
@Override
72+
public void actionPerformed(ActionEvent ev) {
73+
if (!popup.isVisible()) {
74+
postUp();
75+
}
76+
else {
77+
postDown();
78+
}
79+
}
80+
});
81+
82+
f = new JFrame( );
83+
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
84+
f.setLocationRelativeTo(null);
85+
f.setSize(300, 400);
86+
Container c = f.getContentPane( );
87+
c.setLayout(new FlowLayout( ));
88+
c.add(jtb);
89+
f.setVisible(true);
90+
popup = new MyPopupMenu();
91+
popup.add(new JMenuItem("A"));
92+
popup.add(new JMenuItem("B"));
93+
popup.add(new JMenuItem("C"));
94+
popup.setVisible(false);
95+
p = jtb.getLocationOnScreen();
96+
});
97+
98+
robot.waitForIdle();
99+
robot.setAutoDelay(50);
100+
robot.mouseMove(p.x + 15, p.y + 15);
101+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
102+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
103+
104+
robot.waitForIdle();
105+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
106+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
107+
108+
} catch (UnsupportedLookAndFeelException e) {
109+
System.out.println("Note: LookAndFeel " + LF.getClassName()
110+
+ " is not supported on this configuration");
111+
} finally {
112+
if (f != null) {
113+
SwingUtilities.invokeAndWait(() -> f.dispose());
114+
}
115+
}
116+
117+
SwingUtilities.invokeAndWait(() -> {
118+
isPopupVisible = popup.isVisible();
119+
});
120+
121+
if (isPopupVisible) {
122+
throw new RuntimeException("PopupMenu is not taken down after"+
123+
" single button click");
124+
}
125+
}
126+
}
127+
128+
public static void postUp() {
129+
popup.setInvoker(jtb);
130+
popup.setVisible(true);
131+
}
132+
133+
public static void postDown() {
134+
popup.setVisible(false);
135+
}
136+
137+
private static class MyButton extends JButton {
138+
public MyButton(String string) {
139+
super (string);
140+
}
141+
@Override
142+
protected void processMouseEvent(MouseEvent e) {
143+
super.processMouseEvent(e);
144+
}
145+
}
146+
147+
private static class MyPopupMenu extends JPopupMenu {
148+
@Override
149+
public void setVisible( boolean state ) {
150+
if( !state ) {
151+
Exception ex = new Exception();
152+
StringWriter stringWriter = new StringWriter();
153+
PrintWriter printWriter = new PrintWriter( stringWriter );
154+
ex.printStackTrace( printWriter );
155+
String traceString = stringWriter.getBuffer().toString();
156+
if( traceString.lastIndexOf( "windowDeactivated" ) > 0
157+
|| traceString.lastIndexOf( "menuSelectionChanged" )
158+
> 0 ) {
159+
return;
160+
}
161+
}
162+
super.setVisible(state);
163+
}
164+
}
165+
}

0 commit comments

Comments
 (0)