diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ac56367..cd8bd18c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ FlatLaf Change Log `Spinner.buttonStyle` to `button` (default) or `none`). - TableHeader: Support top/bottom/left positioned sort arrow when using [Glazed Lists](https://github.com/glazedlists/glazedlists). (issue #113) +- Button, CheckBox, RadioButton and ToggleButton: Do not paint focus indicator + if `AbstractButton.isFocusPainted()` returns `false`. - Fixed/improved vertical position of text when scaled on HiDPI screens on Windows. diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatCheckBoxIcon.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatCheckBoxIcon.java index 50b1219b..4b4a74e6 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatCheckBoxIcon.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatCheckBoxIcon.java @@ -135,7 +135,7 @@ public class FlatCheckBoxIcon boolean isFocused = FlatUIUtils.isPermanentFocusOwner( c ); // paint focused border - if( isFocused && focusWidth > 0 ) { + if( isFocused && focusWidth > 0 && FlatButtonUI.isFocusPainted( c ) ) { g2.setColor( focusColor ); paintFocusBorder( g2 ); } diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatHelpButtonIcon.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatHelpButtonIcon.java index bc904470..e996f785 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatHelpButtonIcon.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatHelpButtonIcon.java @@ -85,7 +85,7 @@ public class FlatHelpButtonIcon boolean focused = FlatUIUtils.isPermanentFocusOwner( c ); // paint focused border - if( focused ) { + if( focused && FlatButtonUI.isFocusPainted( c ) ) { g2.setColor( focusColor ); g2.fill( new Ellipse2D.Float( 0.5f, 0.5f, iconSize - 1, iconSize - 1 ) ); } diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonBorder.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonBorder.java index 266a4d60..41fb2478 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonBorder.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonBorder.java @@ -81,6 +81,11 @@ public class FlatButtonBorder return FlatButtonUI.isDefaultButton( c ) ? defaultFocusColor : super.getFocusColor( c ); } + @Override + protected boolean isFocused( Component c ) { + return FlatButtonUI.isFocusPainted( c ) && super.isFocused( c ); + } + @Override protected Paint getBorderColor( Component c ) { boolean def = FlatButtonUI.isDefaultButton( c ); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java index b34a9598..d81358fb 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java @@ -229,6 +229,10 @@ public class FlatButtonUI return !(c instanceof AbstractButton) || ((AbstractButton)c).isContentAreaFilled(); } + public static boolean isFocusPainted( Component c ) { + return !(c instanceof AbstractButton) || ((AbstractButton)c).isFocusPainted(); + } + static boolean isDefaultButton( Component c ) { return c instanceof JButton && ((JButton)c).isDefaultButton(); } @@ -315,7 +319,7 @@ public class FlatButtonUI // paint shadow Color shadowColor = def ? defaultShadowColor : this.shadowColor; if( !isToolBarButton && shadowColor != null && shadowWidth > 0 && focusWidth > 0 && - !FlatUIUtils.isPermanentFocusOwner( c ) && c.isEnabled() ) + !(isFocusPainted( c ) && FlatUIUtils.isPermanentFocusOwner( c )) && c.isEnabled() ) { g2.setColor( shadowColor ); g2.fill( new RoundRectangle2D.Float( focusWidth, focusWidth + UIScale.scale( (float) shadowWidth ), @@ -421,7 +425,7 @@ public class FlatButtonUI if( hoverColor != null && b != null && b.getModel().isRollover() ) return hoverColor; - if( focusedColor != null && FlatUIUtils.isPermanentFocusOwner( c ) ) + if( focusedColor != null && isFocusPainted( c ) && FlatUIUtils.isPermanentFocusOwner( c ) ) return focusedColor; return enabledColor; diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java index d6f592dd..85c3bf35 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java @@ -80,6 +80,15 @@ public class FlatComponentsTest } } + private void focusPaintedChanged() { + boolean focusPainted = focusPaintedCheckBox.isSelected(); + + for( Component c : getComponents() ) { + if( c instanceof AbstractButton ) + ((AbstractButton)c).setFocusPainted( focusPainted ); + } + } + private void roundRectChanged() { Boolean roundRect = roundRectCheckBox.isSelected() ? true : null; @@ -262,6 +271,7 @@ public class FlatComponentsTest warningOutlineRadioButton = new JRadioButton(); magentaOutlineRadioButton = new JRadioButton(); magentaCyanOutlineRadioButton = new JRadioButton(); + focusPaintedCheckBox = new JCheckBox(); JLabel scrollBarLabel = new JLabel(); JScrollBar scrollBar1 = new JScrollBar(); JScrollBar scrollBar4 = new JScrollBar(); @@ -1065,6 +1075,7 @@ public class FlatComponentsTest // rows "[]" + "[]" + + "[]" + "[]")); //---- buttonTypeComboBox ---- @@ -1125,7 +1136,13 @@ public class FlatComponentsTest magentaCyanOutlineRadioButton.addActionListener(e -> outlineChanged()); panel4.add(magentaCyanOutlineRadioButton); } - panel5.add(panel4, "cell 0 2"); + panel5.add(panel4, "cell 0 2 1 2"); + + //---- focusPaintedCheckBox ---- + focusPaintedCheckBox.setText("focusPainted"); + focusPaintedCheckBox.setSelected(true); + focusPaintedCheckBox.addActionListener(e -> focusPaintedChanged()); + panel5.add(focusPaintedCheckBox, "cell 1 2"); } add(panel5, "cell 5 13 2 10,grow"); @@ -1366,6 +1383,7 @@ public class FlatComponentsTest private JRadioButton warningOutlineRadioButton; private JRadioButton magentaOutlineRadioButton; private JRadioButton magentaCyanOutlineRadioButton; + private JCheckBox focusPaintedCheckBox; private JSlider slider3; private JProgressBar progressBar1; private JProgressBar progressBar2; diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.jfd b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.jfd index 3980a7d1..e904bb7d 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.jfd +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.jfd @@ -940,7 +940,7 @@ new FormModel { } ) add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { "$columnConstraints": "[][]" - "$rowConstraints": "[][][]" + "$rowConstraints": "[][][][]" "$layoutConstraints": "ltr,insets dialog,hidemode 3" } ) { name: "panel5" @@ -1045,7 +1045,18 @@ new FormModel { addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "outlineChanged", false ) ) } ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 0 2" + "value": "cell 0 2 1 2" + } ) + add( new FormComponent( "javax.swing.JCheckBox" ) { + name: "focusPaintedCheckBox" + "text": "focusPainted" + "selected": true + auxiliary() { + "JavaCodeGenerator.variableLocal": false + } + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "focusPaintedChanged", false ) ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 2" } ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 5 13 2 10,grow"