Button: make icon-only buttons square (no minimum width, smaller left/right insets)

This commit is contained in:
Karl Tauber
2019-10-04 18:13:41 +02:00
parent 26a2446a4d
commit 37c70f6c9e
7 changed files with 50 additions and 1 deletions

View File

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

View File

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

View File

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

View File

@@ -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");

View File

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

View File

@@ -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");

View File

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