From 0c00117820ff65bf814c7f8db0c2d97087782486 Mon Sep 17 00:00:00 2001 From: rogerbj Date: Sat, 13 Jan 2024 07:37:24 +0100 Subject: [PATCH] Remove the dependency with JMenuBar to support, for example, the CommandMenuBar in JIDE OSS (cherry picked from commit 4d4b90c989f2de9f5474bcd13281c6841b513749) --- .../com/formdev/flatlaf/ui/FlatMenuUI.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuUI.java index 4bfa5557..77ce88af 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuUI.java @@ -17,6 +17,7 @@ package com.formdev.flatlaf.ui; import java.awt.Color; +import java.awt.Container; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; @@ -271,7 +272,7 @@ public class FlatMenuUI if( !isHover() ) selectionBackground = getStyleFromMenuBarUI( ui -> ui.selectionBackground, menuBarSelectionBackground, selectionBackground ); - JMenuBar menuBar = (JMenuBar) menuItem.getParent(); + Container menuBar = menuItem.getParent(); JRootPane rootPane = SwingUtilities.getRootPane( menuBar ); if( rootPane != null && rootPane.getParent() instanceof Window && rootPane.getJMenuBar() == menuBar && @@ -321,12 +322,17 @@ public class FlatMenuUI } private T getStyleFromMenuBarUI( Function f, T defaultValue ) { - MenuBarUI ui = ((JMenuBar)menuItem.getParent()).getUI(); - if( !(ui instanceof FlatMenuBarUI) ) - return defaultValue; - - T value = f.apply( (FlatMenuBarUI) ui ); - return (value != null) ? value : defaultValue; + Container menuItemParent = menuItem.getParent(); + if( menuItemParent instanceof JMenuBar ) { + MenuBarUI ui = ((JMenuBar) menuItemParent).getUI(); + if( ui instanceof FlatMenuBarUI ) { + T value = f.apply( (FlatMenuBarUI) ui ); + if( value != null ) { + return value; + } + } + } + return defaultValue; } } }