diff --git a/CHANGELOG.md b/CHANGELOG.md index b1f98c5f..dd68d6d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ FlatLaf Change Log - Table and PopupFactory: Use `StackWalker` in Java 9+ for better performance. (issue #334) - ToolBar: Paint focus indicator for focused button in toolbar. (issue #346) +- ToolBar: Support focusable buttons in toolbar (set UI values + `ToolBar.focusableButtons` to `true`). (issue #346) #### Fixed bugs diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolBarUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolBarUI.java index 49b00e49..86af0906 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolBarUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolBarUI.java @@ -22,6 +22,7 @@ import java.awt.event.ContainerEvent; import java.awt.event.ContainerListener; import javax.swing.AbstractButton; import javax.swing.JComponent; +import javax.swing.UIManager; import javax.swing.border.Border; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicToolBarUI; @@ -41,15 +42,47 @@ import javax.swing.plaf.basic.BasicToolBarUI; * @uiDefault ToolBar.floatingForeground Color * @uiDefault ToolBar.isRollover boolean * + * + * + * @uiDefault ToolBar.focusableButtons boolean + * * @author Karl Tauber */ public class FlatToolBarUI extends BasicToolBarUI { + /** @since 1.4 */ + protected boolean focusableButtons; + public static ComponentUI createUI( JComponent c ) { return new FlatToolBarUI(); } + @Override + public void installUI( JComponent c ) { + super.installUI( c ); + + // disable focusable state of buttons (when switching from another Laf) + if( !focusableButtons ) + setButtonsFocusable( false ); + } + + @Override + public void uninstallUI( JComponent c ) { + super.uninstallUI( c ); + + // re-enable focusable state of buttons (when switching to another Laf) + if( !focusableButtons ) + setButtonsFocusable( true ); + } + + @Override + protected void installDefaults() { + super.installDefaults(); + + focusableButtons = UIManager.getBoolean( "ToolBar.focusableButtons" ); + } + @Override protected ContainerListener createToolBarContListener() { return new ToolBarContListener() { @@ -57,22 +90,36 @@ public class FlatToolBarUI public void componentAdded( ContainerEvent e ) { super.componentAdded( e ); - Component c = e.getChild(); - if( c instanceof AbstractButton ) - c.setFocusable( false ); + if( !focusableButtons ) { + Component c = e.getChild(); + if( c instanceof AbstractButton ) + c.setFocusable( false ); + } } @Override public void componentRemoved( ContainerEvent e ) { super.componentRemoved( e ); - Component c = e.getChild(); - if( c instanceof AbstractButton ) - c.setFocusable( true ); + if( !focusableButtons ) { + Component c = e.getChild(); + if( c instanceof AbstractButton ) + c.setFocusable( true ); + } } }; } + /** + * @since 1.4 + */ + protected void setButtonsFocusable( boolean focusable ) { + for( Component c : toolBar.getComponents() ) { + if( c instanceof AbstractButton ) + c.setFocusable( focusable ); + } + } + // disable rollover border @Override protected void setBorderToRollover( Component c ) {} @Override protected void setBorderToNonRollover( Component c ) {} diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties index 52e8bff9..90c86413 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties @@ -752,6 +752,7 @@ ToolBar.separatorWidth = 7 ToolBar.separatorColor = $Separator.foreground ToolBar.spacingBorder = $Button.toolbar.spacingInsets +ToolBar.focusableButtons = false #---- ToolTipManager ---- diff --git a/flatlaf-testing/dumps/uidefaults/FlatDarkLaf_1.8.0_202.txt b/flatlaf-testing/dumps/uidefaults/FlatDarkLaf_1.8.0_202.txt index 69102d69..8ddfceaa 100644 --- a/flatlaf-testing/dumps/uidefaults/FlatDarkLaf_1.8.0_202.txt +++ b/flatlaf-testing/dumps/uidefaults/FlatDarkLaf_1.8.0_202.txt @@ -1249,6 +1249,7 @@ ToolBar.dockingBackground #3c3f41 javax.swing.plaf.ColorUIResource [UI] ToolBar.dockingForeground #bbbbbb javax.swing.plaf.ColorUIResource [UI] ToolBar.floatingBackground #3c3f41 javax.swing.plaf.ColorUIResource [UI] ToolBar.floatingForeground #888888 javax.swing.plaf.ColorUIResource [UI] +ToolBar.focusableButtons false ToolBar.font [active] $defaultFont [UI] ToolBar.foreground #bbbbbb javax.swing.plaf.ColorUIResource [UI] ToolBar.gripColor #adadad javax.swing.plaf.ColorUIResource [UI] diff --git a/flatlaf-testing/dumps/uidefaults/FlatLightLaf_1.8.0_202.txt b/flatlaf-testing/dumps/uidefaults/FlatLightLaf_1.8.0_202.txt index 272cc8a7..2a118223 100644 --- a/flatlaf-testing/dumps/uidefaults/FlatLightLaf_1.8.0_202.txt +++ b/flatlaf-testing/dumps/uidefaults/FlatLightLaf_1.8.0_202.txt @@ -1254,6 +1254,7 @@ ToolBar.dockingBackground #f2f2f2 javax.swing.plaf.ColorUIResource [UI] ToolBar.dockingForeground #000000 javax.swing.plaf.ColorUIResource [UI] ToolBar.floatingBackground #f2f2f2 javax.swing.plaf.ColorUIResource [UI] ToolBar.floatingForeground #8c8c8c javax.swing.plaf.ColorUIResource [UI] +ToolBar.focusableButtons false ToolBar.font [active] $defaultFont [UI] ToolBar.foreground #000000 javax.swing.plaf.ColorUIResource [UI] ToolBar.gripColor #afafaf javax.swing.plaf.ColorUIResource [UI] diff --git a/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0_202.txt b/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0_202.txt index 62a7b049..833e85f4 100644 --- a/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0_202.txt +++ b/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0_202.txt @@ -1263,6 +1263,7 @@ ToolBar.dockingBackground #ccffcc javax.swing.plaf.ColorUIResource [UI] ToolBar.dockingForeground #ff0000 javax.swing.plaf.ColorUIResource [UI] ToolBar.floatingBackground #ccffcc javax.swing.plaf.ColorUIResource [UI] ToolBar.floatingForeground #000088 javax.swing.plaf.ColorUIResource [UI] +ToolBar.focusableButtons true ToolBar.font [active] $defaultFont [UI] ToolBar.foreground #ff0000 javax.swing.plaf.ColorUIResource [UI] ToolBar.gripColor #afafaf javax.swing.plaf.ColorUIResource [UI] diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties index 589e78dd..3d686a41 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties @@ -400,6 +400,11 @@ ToggleButton.pressedBackground = #FFC800 ToggleButton.toolbar.selectedBackground = #ddd +#---- ToolBar ---- + +ToolBar.focusableButtons = true + + #---- ToolTip ---- ToolTip.background = #eef diff --git a/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt b/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt index d5960513..6c84a82b 100644 --- a/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt +++ b/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt @@ -966,6 +966,7 @@ ToolBar.dockingBackground ToolBar.dockingForeground ToolBar.floatingBackground ToolBar.floatingForeground +ToolBar.focusableButtons ToolBar.font ToolBar.foreground ToolBar.gripColor