diff --git a/CHANGELOG.md b/CHANGELOG.md index f3d25df1..735724d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ FlatLaf Change Log ## Unreleased - Menus: Fixed text color of selected menu items that use HTML. (issue #87) +- Menus: On Windows, pressing F10 now activates the menu bar without + showing a menu popup (as usual on Windows platform). On other platforms the + first menu popup is shown. - Hide mnemonics if window is deactivated (e.g. Alt+Tab to another window). (issue #43) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java index dabcdd6f..8a509a19 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java @@ -16,9 +16,19 @@ package com.formdev.flatlaf.ui; +import java.awt.event.ActionEvent; +import javax.swing.AbstractAction; +import javax.swing.ActionMap; import javax.swing.JComponent; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.MenuElement; +import javax.swing.MenuSelectionManager; +import javax.swing.SwingUtilities; +import javax.swing.plaf.ActionMapUIResource; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicMenuBarUI; +import com.formdev.flatlaf.util.SystemInfo; /** * Provides the Flat LaF UI delegate for {@link javax.swing.JMenuBar}. @@ -38,4 +48,36 @@ public class FlatMenuBarUI public static ComponentUI createUI( JComponent c ) { return new FlatMenuBarUI(); } + + @Override + protected void installKeyboardActions() { + super.installKeyboardActions(); + + if( SystemInfo.IS_WINDOWS ) { + ActionMap map = SwingUtilities.getUIActionMap( menuBar ); + if( map == null ) { + map = new ActionMapUIResource(); + SwingUtilities.replaceUIActionMap( menuBar, map ); + } + map.put( "takeFocus", new TakeFocus() ); + } + } + + //---- class TakeFocus ---------------------------------------------------- + + /** + * On Windows, activates the menu bar, but does not show the menu popup as + * BasicMenuBarUI.TakeFocus does. + */ + private static class TakeFocus + extends AbstractAction + { + @Override + public void actionPerformed( ActionEvent e ) { + JMenuBar menuBar = (JMenuBar) e.getSource(); + JMenu menu = menuBar.getMenu( 0 ); + if( menu != null ) + MenuSelectionManager.defaultManager().setSelectedPath( new MenuElement[] { menuBar, menu } ); + } + } } diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.java b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.java index a698bccc..dc6b0f53 100644 --- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.java +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.java @@ -63,7 +63,7 @@ class ControlBar (SystemInfo.IS_LINUX && className.equals( "com.sun.java.swing.plaf.gtk.GTKLookAndFeel") ) ) name += " (F9)"; else if( className.equals( MetalLookAndFeel.class.getName() ) ) - name += " (F10)"; + name += " (F12)"; else if( className.equals( NimbusLookAndFeel.class.getName() ) ) name += " (F11)"; @@ -106,7 +106,7 @@ class ControlBar registerSwitchToLookAndFeel( KeyEvent.VK_F9, "com.apple.laf.AquaLookAndFeel" ); else if( SystemInfo.IS_LINUX ) registerSwitchToLookAndFeel( KeyEvent.VK_F9, "com.sun.java.swing.plaf.gtk.GTKLookAndFeel" ); - registerSwitchToLookAndFeel( KeyEvent.VK_F10, MetalLookAndFeel.class.getName() ); + registerSwitchToLookAndFeel( KeyEvent.VK_F12, MetalLookAndFeel.class.getName() ); registerSwitchToLookAndFeel( KeyEvent.VK_F11, NimbusLookAndFeel.class.getName() ); // register ESC key to close frame