From 60856f5875b59d9bfed3faec4b569df5632fa8e0 Mon Sep 17 00:00:00 2001 From: Prasanta Sadhukhan Date: Fri, 5 Dec 2025 02:44:49 +0000 Subject: [PATCH 1/6] 6726690: SwingUtilities.replaceUI*Map() methods do not remove previously installed maps --- .../classes/javax/swing/SwingUtilities.java | 12 ++++ .../javax/swing/SwingUtilities/UIMapTest.java | 68 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 test/jdk/javax/swing/SwingUtilities/UIMapTest.java diff --git a/src/java.desktop/share/classes/javax/swing/SwingUtilities.java b/src/java.desktop/share/classes/javax/swing/SwingUtilities.java index 4633e9c47565f..d1ab661d80744 100644 --- a/src/java.desktop/share/classes/javax/swing/SwingUtilities.java +++ b/src/java.desktop/share/classes/javax/swing/SwingUtilities.java @@ -1815,10 +1815,16 @@ public static void replaceUIInputMap(JComponent component, int type, InputMap parent = map.getParent(); if (parent == null || (parent instanceof UIResource)) { map.setParent(uiInputMap); + if (uiInputMap == null) { + map.clear(); + } return; } map = parent; } + if (map != null && uiInputMap == null) { + map.clear(); + } } @@ -1839,10 +1845,16 @@ public static void replaceUIActionMap(JComponent component, ActionMap parent = map.getParent(); if (parent == null || (parent instanceof UIResource)) { map.setParent(uiActionMap); + if (uiActionMap == null) { + map.clear(); + } return; } map = parent; } + if (map != null && uiActionMap == null) { + map.clear(); + } } diff --git a/test/jdk/javax/swing/SwingUtilities/UIMapTest.java b/test/jdk/javax/swing/SwingUtilities/UIMapTest.java new file mode 100644 index 0000000000000..a5d6b611b554b --- /dev/null +++ b/test/jdk/javax/swing/SwingUtilities/UIMapTest.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 6726690 + * @summary Verifies SwingUtilities.replaceUI*Map() methods remove + * previously installed maps + * @run main UIMapTest + */ + +import java.awt.event.KeyEvent; +import javax.swing.ComponentInputMap; +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JFrame; +import javax.swing.KeyStroke; +import javax.swing.SwingUtilities; + +public class UIMapTest { + public static void main(String[] args) { + // Create the test button + JButton button = new JButton("Test"); + + // Create an input map that maps ENTER to the button + ComponentInputMap map = new ComponentInputMap(button); + map.put(KeyStroke.getKeyStroke("pressed ENTER"), "pressed"); + map.put(KeyStroke.getKeyStroke("released ENTER"), "released"); + + // Add the map + SwingUtilities.replaceUIInputMap(button, JComponent.WHEN_IN_FOCUSED_WINDOW, map); + + // Attempt to remove the map + SwingUtilities.replaceUIInputMap(button, JComponent.WHEN_IN_FOCUSED_WINDOW, null); + + // Show the frame + JFrame frame = new JFrame(); + frame.add(button); + // frame.pack(); + //frame.setVisible(true); + + if (button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW). + get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)) != null) { + throw new RuntimeException("SwingUtilities.replaceUIInputMap " + + "didn't remove previously installed map"); + } + } +} From a2fe949b3ad7e8149b5adbe0d7948933c8d9fd5f Mon Sep 17 00:00:00 2001 From: Prasanta Sadhukhan Date: Fri, 5 Dec 2025 02:47:48 +0000 Subject: [PATCH 2/6] Test fix --- test/jdk/javax/swing/SwingUtilities/UIMapTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/jdk/javax/swing/SwingUtilities/UIMapTest.java b/test/jdk/javax/swing/SwingUtilities/UIMapTest.java index a5d6b611b554b..1a78bfae0938b 100644 --- a/test/jdk/javax/swing/SwingUtilities/UIMapTest.java +++ b/test/jdk/javax/swing/SwingUtilities/UIMapTest.java @@ -54,10 +54,8 @@ public static void main(String[] args) { SwingUtilities.replaceUIInputMap(button, JComponent.WHEN_IN_FOCUSED_WINDOW, null); // Show the frame - JFrame frame = new JFrame(); + JFrame frame = new JFrame("UIMapTest"); frame.add(button); - // frame.pack(); - //frame.setVisible(true); if (button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW). get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)) != null) { From 63a5ac1ce59f8c6eb962b89a590b50e6b0e09bf0 Mon Sep 17 00:00:00 2001 From: Prasanta Sadhukhan Date: Fri, 5 Dec 2025 03:06:06 +0000 Subject: [PATCH 3/6] Test fix --- test/jdk/javax/swing/SwingUtilities/UIMapTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/jdk/javax/swing/SwingUtilities/UIMapTest.java b/test/jdk/javax/swing/SwingUtilities/UIMapTest.java index 1a78bfae0938b..084ab34ae232d 100644 --- a/test/jdk/javax/swing/SwingUtilities/UIMapTest.java +++ b/test/jdk/javax/swing/SwingUtilities/UIMapTest.java @@ -59,7 +59,7 @@ public static void main(String[] args) { if (button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW). get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)) != null) { - throw new RuntimeException("SwingUtilities.replaceUIInputMap " + + throw new RuntimeException("SwingUtilities.replaceUIInputMap " + "didn't remove previously installed map"); } } From 002b018f1d19a0e33a3d2ccfc64335a30d51eeab Mon Sep 17 00:00:00 2001 From: Prasanta Sadhukhan Date: Mon, 8 Dec 2025 13:10:56 +0000 Subject: [PATCH 4/6] Redundant code remove --- src/java.desktop/share/classes/javax/swing/SwingUtilities.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/SwingUtilities.java b/src/java.desktop/share/classes/javax/swing/SwingUtilities.java index d1ab661d80744..2fe25c60419f1 100644 --- a/src/java.desktop/share/classes/javax/swing/SwingUtilities.java +++ b/src/java.desktop/share/classes/javax/swing/SwingUtilities.java @@ -1822,9 +1822,6 @@ public static void replaceUIInputMap(JComponent component, int type, } map = parent; } - if (map != null && uiInputMap == null) { - map.clear(); - } } From 847b6ba9cd4f72bf9c8bbcd4c14e9da0b1338be2 Mon Sep 17 00:00:00 2001 From: Prasanta Sadhukhan Date: Mon, 8 Dec 2025 13:12:40 +0000 Subject: [PATCH 5/6] Redundant code remove --- src/java.desktop/share/classes/javax/swing/SwingUtilities.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/SwingUtilities.java b/src/java.desktop/share/classes/javax/swing/SwingUtilities.java index 2fe25c60419f1..e113378467ba8 100644 --- a/src/java.desktop/share/classes/javax/swing/SwingUtilities.java +++ b/src/java.desktop/share/classes/javax/swing/SwingUtilities.java @@ -1849,9 +1849,6 @@ public static void replaceUIActionMap(JComponent component, } map = parent; } - if (map != null && uiActionMap == null) { - map.clear(); - } } From 85028272d3ca160c49f1f5211d74b6502f9d2bd4 Mon Sep 17 00:00:00 2001 From: Prasanta Sadhukhan Date: Tue, 9 Dec 2025 00:32:07 +0000 Subject: [PATCH 6/6] Fix update...ActionMap test added --- .../classes/javax/swing/SwingUtilities.java | 12 +++---- .../javax/swing/SwingUtilities/UIMapTest.java | 36 +++++++++++++++---- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/SwingUtilities.java b/src/java.desktop/share/classes/javax/swing/SwingUtilities.java index e113378467ba8..70fbeee8c7bb5 100644 --- a/src/java.desktop/share/classes/javax/swing/SwingUtilities.java +++ b/src/java.desktop/share/classes/javax/swing/SwingUtilities.java @@ -1813,11 +1813,11 @@ public static void replaceUIInputMap(JComponent component, int type, while (map != null) { InputMap parent = map.getParent(); + if (uiInputMap == null) { + map.clear(); + } if (parent == null || (parent instanceof UIResource)) { map.setParent(uiInputMap); - if (uiInputMap == null) { - map.clear(); - } return; } map = parent; @@ -1840,11 +1840,11 @@ public static void replaceUIActionMap(JComponent component, while (map != null) { ActionMap parent = map.getParent(); + if (uiActionMap == null) { + map.clear(); + } if (parent == null || (parent instanceof UIResource)) { map.setParent(uiActionMap); - if (uiActionMap == null) { - map.clear(); - } return; } map = parent; diff --git a/test/jdk/javax/swing/SwingUtilities/UIMapTest.java b/test/jdk/javax/swing/SwingUtilities/UIMapTest.java index 084ab34ae232d..4a801580b35c8 100644 --- a/test/jdk/javax/swing/SwingUtilities/UIMapTest.java +++ b/test/jdk/javax/swing/SwingUtilities/UIMapTest.java @@ -29,8 +29,13 @@ * @run main UIMapTest */ +import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; +import javax.swing.AbstractAction; +import javax.swing.Action; +import javax.swing.ActionMap; import javax.swing.ComponentInputMap; +import javax.swing.InputMap; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; @@ -38,7 +43,11 @@ import javax.swing.SwingUtilities; public class UIMapTest { + public static void main(String[] args) { + + StringBuilder str = new StringBuilder(); + // Create the test button JButton button = new JButton("Test"); @@ -53,14 +62,29 @@ public static void main(String[] args) { // Attempt to remove the map SwingUtilities.replaceUIInputMap(button, JComponent.WHEN_IN_FOCUSED_WINDOW, null); - // Show the frame - JFrame frame = new JFrame("UIMapTest"); - frame.add(button); - if (button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW). get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)) != null) { - throw new RuntimeException("SwingUtilities.replaceUIInputMap " + - "didn't remove previously installed map"); + str.append("\nSwingUtilities.replaceUIInputMap " + + "didn't remove previously installed input map"); + } + + // Get the InputMap for the button when it has focus + InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); + + // Map the VK_ENTER key stroke to a specific action name + inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "doEnterAction"); + Action enterAction = new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { } + }; + button.getActionMap().put("doEnterAction", enterAction); + SwingUtilities.replaceUIActionMap(button, null); + if (button.getActionMap().size() != 0) { + str.append("\nSwingUtilities.replaceUIActionMap " + + "didn't remove previously installed action map"); + } + if (str.length() != 0) { + throw new RuntimeException(str.toString()); } } }