Skip to content

Commit f5d1b5b

Browse files
committedAug 1, 2022
6463708: DefaultButtonModel.setMnemonic generates ChangeEvent for no change
Reviewed-by: azvegint, prr
1 parent 30205bb commit f5d1b5b

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed
 

‎src/java.desktop/share/classes/javax/swing/DefaultButtonModel.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,10 @@ public void setRollover(boolean b) {
290290
* {@inheritDoc}
291291
*/
292292
public void setMnemonic(int key) {
293-
mnemonic = key;
294-
fireStateChanged();
293+
if (this.mnemonic != key) {
294+
this.mnemonic = key;
295+
fireStateChanged();
296+
}
295297
}
296298

297299
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2022, 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+
/*
25+
* @test
26+
* @bug 6463708
27+
* @summary Verifies if DefaultButtonModel.setMnemonic
28+
* generates ChangeEvent for no change
29+
* @run main TestMnemonicEvent
30+
*/
31+
32+
import javax.swing.DefaultButtonModel;
33+
import javax.swing.event.ChangeEvent;
34+
import javax.swing.event.ChangeListener;
35+
36+
public class TestMnemonicEvent implements ChangeListener {
37+
38+
private static ChangeEvent lastEvent;
39+
40+
public void stateChanged(ChangeEvent e) {
41+
lastEvent = e;
42+
}
43+
44+
public static void main(String[] args) {
45+
TestMnemonicEvent test = new TestMnemonicEvent();
46+
DefaultButtonModel m = new DefaultButtonModel();
47+
m.addChangeListener(test);
48+
m.setMnemonic(70);
49+
System.out.println("Event triggered: " + (lastEvent != null));
50+
51+
// clear the recorded event, set the same mnemonic then check if another
52+
// event is triggered...
53+
lastEvent = null;
54+
m.setMnemonic(70);
55+
System.out.println("Event triggered: " + (lastEvent != null));
56+
if (lastEvent != null) {
57+
throw new RuntimeException("ChangeEvent triggered for same mnemonic");
58+
}
59+
}
60+
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Aug 1, 2022

@openjdk-notifier[bot]
Please sign in to comment.