diff --git a/CHANGELOG.md b/CHANGELOG.md index ce295f16..43bc8a18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ FlatLaf Change Log - Use new chevron arrows in "Flat Light" and "Flat Dark" themes, but keep triangle arrows in "Flat IntelliJ" and "Flat Darcula" themes. (issue #7) +- If a JButton has an icon and no text, then it does not get a minimum width + (usually 72 pixel) and the left and right insets are same as top/bottom insets + so that it becomes square (if the icon is square). - Added Java 9 module descriptor `module-info.class` to `flatlaf.jar` (in `META-INF/versions/9`). But FlatLaf remains Java 8 compatible. (issue #1) - Support specifying custom scale factor in system properties `flatlaf.uiScale` 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 2d2b31ae..5a982baf 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 @@ -20,8 +20,11 @@ import static com.formdev.flatlaf.util.UIScale.scale; import java.awt.Color; import java.awt.Component; import java.awt.Graphics; +import java.awt.Insets; import java.awt.Paint; +import javax.swing.JButton; import javax.swing.UIManager; +import javax.swing.plaf.UIResource; /** * Border for {@link javax.swing.JButton}. @@ -73,6 +76,17 @@ public class FlatButtonBorder null ); } + @Override + public Insets getBorderInsets( Component c, Insets insets ) { + insets = super.getBorderInsets( c, insets ); + + // use smaller left and right insets for icon-only buttons (so that they are square) + if( FlatButtonUI.isIconOnlyButton( c ) && ((JButton)c).getMargin() instanceof UIResource ) + insets.left = insets.right = Math.min( insets.top, insets.bottom ); + + return insets; + } + @Override protected float getArc() { return scale( (float) arc ); 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 239cca42..44e496b3 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 @@ -146,6 +146,13 @@ public class FlatButtonUI return c instanceof JButton && ((JButton)c).isDefaultButton(); } + static boolean isIconOnlyButton( Component c ) { + String text; + return c instanceof JButton && + ((JButton)c).getIcon() != null && + ((text = ((JButton)c).getText()) == null || text.isEmpty()); + } + static boolean isHelpButton( Component c ) { return c instanceof JButton && clientPropertyEquals( (JButton) c, BUTTON_TYPE, BUTTON_TYPE_HELP ); } @@ -259,8 +266,11 @@ public class FlatButtonUI return new Dimension( helpButtonIcon.getIconWidth(), helpButtonIcon.getIconHeight() ); Dimension prefSize = super.getPreferredSize( c ); - if( !isToolBarButton( c ) ) + + // apply minimum width, if not in toolbar and not a icon-only button + if( !isToolBarButton( c ) && !isIconOnlyButton( c ) ) prefSize.width = Math.max( prefSize.width, scale( minimumWidth + (focusWidth * 2) ) ); + return prefSize; } } diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatComponentsTest.java b/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatComponentsTest.java index 684dcbf9..a346d949 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatComponentsTest.java +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatComponentsTest.java @@ -55,6 +55,7 @@ public class FlatComponentsTest FlatComponentsTest.TestDefaultButton button5 = new FlatComponentsTest.TestDefaultButton(); JButton button3 = new JButton(); JButton button12 = new JButton(); + JButton button13 = new JButton(); JLabel toggleButtonLabel = new JLabel(); JToggleButton toggleButton1 = new JToggleButton(); JToggleButton toggleButton2 = new JToggleButton(); @@ -246,6 +247,10 @@ public class FlatComponentsTest button12.setEnabled(false); add(button12, "cell 4 1"); + //---- button13 ---- + button13.setIcon(UIManager.getIcon("Tree.closedIcon")); + add(button13, "cell 5 1"); + //---- toggleButtonLabel ---- toggleButtonLabel.setText("JToggleButton:"); add(toggleButtonLabel, "cell 0 2"); diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatComponentsTest.jfd b/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatComponentsTest.jfd index 49447ece..f9b23011 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatComponentsTest.jfd +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatComponentsTest.jfd @@ -76,6 +76,12 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 4 1" } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "button13" + "icon": new com.jformdesigner.model.SwingIcon( 2, "Tree.closedIcon" ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 5 1" + } ) add( new FormComponent( "javax.swing.JLabel" ) { name: "toggleButtonLabel" "text": "JToggleButton:" diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/BasicComponentsPanel.java b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/BasicComponentsPanel.java index 820f79b6..f23d5d19 100644 --- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/BasicComponentsPanel.java +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/BasicComponentsPanel.java @@ -39,6 +39,7 @@ class BasicComponentsPanel JButton button2 = new JButton(); JButton button3 = new JButton(); JButton button4 = new JButton(); + JButton button13 = new JButton(); JLabel checkBoxLabel = new JLabel(); JCheckBox checkBox1 = new JCheckBox(); JCheckBox checkBox2 = new JCheckBox(); @@ -169,6 +170,10 @@ class BasicComponentsPanel button4.setEnabled(false); add(button4, "cell 4 1"); + //---- button13 ---- + button13.setIcon(UIManager.getIcon("Tree.closedIcon")); + add(button13, "cell 5 1"); + //---- checkBoxLabel ---- checkBoxLabel.setText("JCheckBox"); add(checkBoxLabel, "cell 0 2"); diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/BasicComponentsPanel.jfd b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/BasicComponentsPanel.jfd index d6ad5c75..5237d298 100644 --- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/BasicComponentsPanel.jfd +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/BasicComponentsPanel.jfd @@ -69,6 +69,12 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 4 1" } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "button13" + "icon": new com.jformdesigner.model.SwingIcon( 2, "Tree.closedIcon" ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 5 1" + } ) add( new FormComponent( "javax.swing.JLabel" ) { name: "checkBoxLabel" "text": "JCheckBox"