RFR: 6726690: SwingUtilities.replaceUI*Map() methods do not remove previously installed maps [v2]

Alexander Zvegintsev azvegint at openjdk.org
Mon Dec 8 14:57:26 UTC 2025


On Mon, 8 Dec 2025 13:18:50 GMT, Prasanta Sadhukhan <psadhukhan at openjdk.org> wrote:

>> `SwingUtilities.replaceUIInputMap()` and `SwingUtilities.replaceUIActionMap()` do not actually remove previously installed maps as their Javadoc indicates if `null` is passed as `uiInputMap`/`uiActionMap`
>> 
>> https://github.com/openjdk/jdk/blob/7e91d34f3e83b4c39d6ce5de34373d7d74d54512/src/java.desktop/share/classes/javax/swing/SwingUtilities.java#L1802-L1803
>> https://github.com/openjdk/jdk/blob/7e91d34f3e83b4c39d6ce5de34373d7d74d54512/src/java.desktop/share/classes/javax/swing/SwingUtilities.java#L1827-L1828
>> 
>> If the passed  `uiInputMap`/`uiActionMap` is null, `JComponent` actually doesn't create a fresh map and returns the previously installed map 
>> https://github.com/openjdk/jdk/blob/7e91d34f3e83b4c39d6ce5de34373d7d74d54512/src/java.desktop/share/classes/javax/swing/JComponent.java#L2586-L2595
>> which is in contradiction to the `replaceUI*Map` spec so `SwingUtilities `needs to clear the previously installed map which is being done in this fix.
>
> Prasanta Sadhukhan has updated the pull request incrementally with two additional commits since the last revision:
> 
>  - Redundant code remove
>  - Redundant code remove

src/java.desktop/share/classes/javax/swing/SwingUtilities.java line 1820:

> 1818:                 if (uiInputMap == null) {
> 1819:                     map.clear();
> 1820:                 }

What if `if (parent == null || (parent instanceof UIResource)) {` is false and `uiInputMap == null`?
Should we also clear the map?

test/jdk/javax/swing/SwingUtilities/UIMapTest.java line 51:

> 49: 
> 50:         // Add the map
> 51:         SwingUtilities.replaceUIInputMap(button, JComponent.WHEN_IN_FOCUSED_WINDOW, map);

Although the fix changes both `replaceUIInputMap` and `replaceUIActionMap`, only `replaceUIInputMap` is tested.

test/jdk/javax/swing/SwingUtilities/UIMapTest.java line 58:

> 56:         // Show the frame
> 57:         JFrame frame = new JFrame("UIMapTest");
> 58:         frame.add(button);

Do we really need this?

Suggestion:



In its current state, the test will fail on headless systems because the test does not have the `@key headful`.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/28671#discussion_r2598937878
PR Review Comment: https://git.openjdk.org/jdk/pull/28671#discussion_r2598935925
PR Review Comment: https://git.openjdk.org/jdk/pull/28671#discussion_r2598928391


More information about the client-libs-dev mailing list