ToolBar: support focusable buttons in toolbar (issue #346)

fixed focusable state when switching to/from other Laf
This commit is contained in:
Karl Tauber
2021-07-10 13:32:30 +02:00
parent 4d8b544aed
commit 8b585deb78
8 changed files with 65 additions and 6 deletions

View File

@@ -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

View File

@@ -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
*
* <!-- FlatToolBarUI -->
*
* @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 ) {}

View File

@@ -752,6 +752,7 @@ ToolBar.separatorWidth = 7
ToolBar.separatorColor = $Separator.foreground
ToolBar.spacingBorder = $Button.toolbar.spacingInsets
ToolBar.focusableButtons = false
#---- ToolTipManager ----

View File

@@ -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]

View File

@@ -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]

View File

@@ -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]

View File

@@ -400,6 +400,11 @@ ToggleButton.pressedBackground = #FFC800
ToggleButton.toolbar.selectedBackground = #ddd
#---- ToolBar ----
ToolBar.focusableButtons = true
#---- ToolTip ----
ToolTip.background = #eef

View File

@@ -966,6 +966,7 @@ ToolBar.dockingBackground
ToolBar.dockingForeground
ToolBar.floatingBackground
ToolBar.floatingForeground
ToolBar.focusableButtons
ToolBar.font
ToolBar.foreground
ToolBar.gripColor