Skip to content

Commit ee5ced8

Browse files
author
duke
committed
Backport 8d33ea7395e5dd504b899d8972617f6696546d84
1 parent f1d867d commit ee5ced8

File tree

5 files changed

+115
-39
lines changed

5 files changed

+115
-39
lines changed

src/java.desktop/share/classes/javax/swing/plaf/basic/BasicPasswordFieldUI.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025, 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
@@ -91,23 +91,4 @@ protected void installDefaults() {
9191
public View create(Element elem) {
9292
return new PasswordView(elem);
9393
}
94-
95-
/**
96-
* Create the action map for Password Field. This map provides
97-
* same actions for double mouse click and
98-
* and for triple mouse click (see bug 4231444).
99-
*/
100-
101-
ActionMap createActionMap() {
102-
ActionMap map = super.createActionMap();
103-
if (map.get(DefaultEditorKit.selectWordAction) != null) {
104-
Action a = map.get(DefaultEditorKit.selectLineAction);
105-
if (a != null) {
106-
map.remove(DefaultEditorKit.selectWordAction);
107-
map.put(DefaultEditorKit.selectWordAction, a);
108-
}
109-
}
110-
return map;
111-
}
112-
11394
}

src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTextUI.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025, 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
@@ -647,6 +647,22 @@ ActionMap createActionMap() {
647647
TransferHandler.getCopyAction());
648648
map.put(TransferHandler.getPasteAction().getValue(Action.NAME),
649649
TransferHandler.getPasteAction());
650+
651+
if (getComponent() instanceof JPasswordField) {
652+
// Edit the action map for Password Field. This map provides
653+
// same actions for double mouse click and
654+
// and for triple mouse click (see bugs 4231444, 8354646).
655+
656+
if (map.get(DefaultEditorKit.selectWordAction) != null) {
657+
map.remove(DefaultEditorKit.selectWordAction);
658+
659+
Action a = map.get(DefaultEditorKit.selectLineAction);
660+
if (a != null) {
661+
map.put(DefaultEditorKit.selectWordAction, a);
662+
}
663+
}
664+
}
665+
650666
return map;
651667
}
652668

src/java.desktop/share/classes/javax/swing/plaf/synth/SynthPasswordFieldUI.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2025, 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
@@ -108,19 +108,4 @@ public void paintBorder(SynthContext context, Graphics g, int x,
108108
int y, int w, int h) {
109109
context.getPainter().paintPasswordFieldBorder(context, g, x, y, w, h);
110110
}
111-
112-
/**
113-
* {@inheritDoc}
114-
*/
115-
@Override
116-
protected void installKeyboardActions() {
117-
super.installKeyboardActions();
118-
ActionMap map = SwingUtilities.getUIActionMap(getComponent());
119-
if (map != null && map.get(DefaultEditorKit.selectWordAction) != null) {
120-
Action a = map.get(DefaultEditorKit.selectLineAction);
121-
if (a != null) {
122-
map.put(DefaultEditorKit.selectWordAction, a);
123-
}
124-
}
125-
}
126111
}

test/jdk/java/awt/TextField/SetEchoCharWordOpsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2025, 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
@@ -30,7 +30,7 @@
3030

3131
/*
3232
* @test
33-
* @bug 6191897
33+
* @bug 6191897 8354646
3434
* @summary Verifies that ctrl+left/right does not move word-by-word in a TextField
3535
* with echo character set
3636
* @library /java/awt/regtesthelpers /test/lib
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 2025, 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+
* @key headful
27+
* @bug 4231444 8354646
28+
* @summary Password fields' ActionMap needs to replace
29+
* DefaultEditorKit.selectWordAction with
30+
* DefaultEditorKit.selectLineAction.
31+
*
32+
* @run main PasswordSelectionWordTest
33+
*/
34+
35+
import javax.swing.Action;
36+
import javax.swing.JPasswordField;
37+
import javax.swing.SwingUtilities;
38+
import javax.swing.UIManager;
39+
import javax.swing.UnsupportedLookAndFeelException;
40+
import javax.swing.plaf.basic.BasicTextUI;
41+
import javax.swing.text.DefaultEditorKit;
42+
import java.awt.event.ActionEvent;
43+
44+
public class PasswordSelectionWordTest {
45+
public static void main(String[] args) throws Exception {
46+
for (UIManager.LookAndFeelInfo laf :
47+
UIManager.getInstalledLookAndFeels()) {
48+
System.out.println("Testing LAF: " + laf.getClassName());
49+
SwingUtilities.invokeAndWait(() -> {
50+
if (setLookAndFeel(laf)) {
51+
runTest();
52+
}
53+
});
54+
}
55+
}
56+
57+
private static boolean setLookAndFeel(UIManager.LookAndFeelInfo laf) {
58+
try {
59+
UIManager.setLookAndFeel(laf.getClassName());
60+
return true;
61+
} catch (UnsupportedLookAndFeelException e) {
62+
System.err.println("Skipping unsupported look and feel:");
63+
e.printStackTrace();
64+
return false;
65+
} catch (Exception e) {
66+
throw new RuntimeException(e);
67+
}
68+
}
69+
70+
private static void runTest() {
71+
String str = "one two three";
72+
JPasswordField field = new JPasswordField(str);
73+
if (!(field.getUI() instanceof BasicTextUI)) {
74+
throw new RuntimeException("Unexpected condition: JPasswordField UI was " + field.getUI());
75+
}
76+
System.out.println("Testing " + field.getUI());
77+
78+
// do something (anything) to initialize the Views:
79+
field.setSize(100, 100);
80+
field.addNotify();
81+
82+
Action action = field.getActionMap().get(
83+
DefaultEditorKit.selectWordAction);
84+
action.actionPerformed(new ActionEvent(field, 0, ""));
85+
int selectionStart = field.getSelectionStart();
86+
int selectionEnd = field.getSelectionEnd();
87+
System.out.println("selectionStart = " + selectionStart);
88+
System.out.println("selectionEnd = " + selectionEnd);
89+
if (selectionStart != 0 || selectionEnd != str.length()) {
90+
throw new RuntimeException("selectionStart = " + selectionStart +
91+
" and selectionEnd = " + selectionEnd);
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)