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

Alexander Zvegintsev azvegint at openjdk.org
Tue Dec 9 09:06:03 UTC 2025


On Tue, 9 Dec 2025 00:36:21 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 one additional commit since the last revision:
> 
>   Fix update...ActionMap test added

I assume that testing looks good on all platforms.

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

> 1837:     public static void replaceUIActionMap(JComponent component,
> 1838:                                           ActionMap uiActionMap) {
> 1839:         ActionMap map = component.getActionMap((uiActionMap != null));

Another approach would be to nullify the map.
In this case we don't modify the previously stored map. If a user stores a link to it, they can reuse it later. 

Suggestion:

if (uiActionMap == null) {
    component.setActionMap(null);
    return;
}

ActionMap map = component.getActionMap(true);

However, this will also require updating the JComponent code to reset the flags.

https://github.com/openjdk/jdk/blob/cba09cd10d4e4482852a317786242836419c313b/src/java.desktop/share/classes/javax/swing/JComponent.java#L2524-L2527


It may be worth considering this option.

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

Marked as reviewed by azvegint (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/28671#pullrequestreview-3556234129
PR Review Comment: https://git.openjdk.org/jdk/pull/28671#discussion_r2601704720


More information about the client-libs-dev mailing list