From 1e93deab2aa2db7611fc6ab878159164778a6a39 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Tue, 5 Oct 2021 15:34:23 +0200 Subject: [PATCH] ToolBar: fixed focus moving to next button when switching Laf and focusable buttons are enabled via CSS style `focusableButtons: true` also avoid unnecessary invokation of `c.setFocusable()` --- .../com/formdev/flatlaf/ui/FlatToolBarUI.java | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) 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 c4a83cff..87f7dc72 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 @@ -83,11 +83,13 @@ public class FlatToolBarUI public void installUI( JComponent c ) { super.installUI( c ); + installStyle(); + // disable focusable state of buttons (when switching from another Laf) + // do this after applying style to avoid disabling (here) and re-enabling + // (in applyStyle()), which would transfer focus to next button if( !focusableButtons ) setButtonsFocusable( false ); - - installStyle(); } @Override @@ -132,22 +134,16 @@ public class FlatToolBarUI public void componentAdded( ContainerEvent e ) { super.componentAdded( e ); - if( !focusableButtons ) { - Component c = e.getChild(); - if( c instanceof AbstractButton ) - c.setFocusable( false ); - } + if( !focusableButtons ) + setButtonFocusable( e.getChild(), false ); } @Override public void componentRemoved( ContainerEvent e ) { super.componentRemoved( e ); - if( !focusableButtons ) { - Component c = e.getChild(); - if( c instanceof AbstractButton ) - c.setFocusable( true ); - } + if( !focusableButtons ) + setButtonFocusable( e.getChild(), true ); } }; } @@ -189,10 +185,13 @@ public class FlatToolBarUI /** @since 1.4 */ protected void setButtonsFocusable( boolean focusable ) { - for( Component c : toolBar.getComponents() ) { - if( c instanceof AbstractButton ) - c.setFocusable( focusable ); - } + for( Component c : toolBar.getComponents() ) + setButtonFocusable( c, focusable ); + } + + private void setButtonFocusable( Component c, boolean focusable ) { + if( c instanceof AbstractButton && focusable != c.isFocusable() ) + c.setFocusable( focusable ); } /**