diff --git a/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/MenuBarSkin.java b/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/MenuBarSkin.java --- a/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/MenuBarSkin.java +++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/MenuBarSkin.java @@ -52,6 +52,8 @@ import javafx.scene.input.MouseEvent; import javafx.scene.layout.HBox; import javafx.stage.Stage; + +import java.lang.ref.SoftReference; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -83,7 +85,7 @@ private TraversalEngine engine; private Direction direction; - private static WeakHashMap systemMenuMap; + private static WeakHashMap> systemMenuMap; private static List wrappedDefaultMenus = new ArrayList(); private static Stage currentMenuBarStage; private List wrappedMenus; @@ -105,10 +107,16 @@ } } + private static MenuBarSkin getMenuBarSkin(Stage stage) { + if (systemMenuMap == null) return null; + SoftReference skinRef = systemMenuMap.get(stage); + return skinRef == null ? null : skinRef.get(); + } + private static void setSystemMenu(Stage stage) { if (stage != null && stage.isFocused()) { while (stage != null && stage.getOwner() instanceof Stage) { - MenuBarSkin skin = systemMenuMap.get(stage); + MenuBarSkin skin = getMenuBarSkin(stage); if (skin != null && skin.wrappedMenus != null) { break; } else { @@ -126,7 +134,7 @@ if (stage != currentMenuBarStage) { List menuList = null; if (stage != null) { - MenuBarSkin skin = systemMenuMap.get(stage); + MenuBarSkin skin = getMenuBarSkin(stage); if (skin != null) { menuList = skin.wrappedMenus; } @@ -140,7 +148,7 @@ } private static void initSystemMenuBar() { - systemMenuMap = new WeakHashMap(); + systemMenuMap = new WeakHashMap<>(); final InvalidationListener focusedStageListener = new InvalidationListener() { @Override public void invalidated(Observable ov) { @@ -531,7 +539,7 @@ Scene scene = getSkinnable().getScene(); if (scene.getWindow() instanceof Stage) { Stage stage = (Stage)scene.getWindow(); - MenuBarSkin curMBSkin = (systemMenuMap != null) ? systemMenuMap.get(stage) : null; + MenuBarSkin curMBSkin = getMenuBarSkin(stage); if (getSkinnable().isUseSystemMenuBar() && !menusContainCustomMenuItem()) { if (curMBSkin != null && (curMBSkin.getSkinnable().getScene() == null || curMBSkin.getSkinnable().getScene().getWindow() == null)) { @@ -547,8 +555,8 @@ initSystemMenuBar(); } if (wrappedMenus == null) { - wrappedMenus = new ArrayList(); - systemMenuMap.put(stage, this); + wrappedMenus = new ArrayList<>(); + systemMenuMap.put(stage, new SoftReference<>(this)); } else { wrappedMenus.clear(); }