Button and ToggleButton: support making buttons square (issue #118)

This commit is contained in:
Karl Tauber
2020-06-29 10:49:07 +02:00
parent df7f693cf4
commit 7b11e29122
3 changed files with 21 additions and 7 deletions

View File

@@ -9,6 +9,8 @@ FlatLaf Change Log
- Button and ToggleButton: Support disabled background color (use UI values
`Button.disabledBackground` and `ToggleButton.disabledBackground`). (issue
#112)
- Button and ToggleButton: Support making buttons square (set client property
`JButton.squareSize` to `true`). (issue #118)
- ScrollBar: Support pressed track, thumb and button colors (use UI values
`ScrollBar.pressedTrackColor`, `ScrollBar.pressedThumbColor` and
`ScrollBar.pressedButtonBackground`). (issue #115)

View File

@@ -96,6 +96,14 @@ public interface FlatClientProperties
*/
String SELECTED_STATE_INDETERMINATE = "indeterminate";
/**
* Specifies whether the button preferred size will be made square (quadratically).
* <p>
* <strong>Components</strong> {@link javax.swing.JButton} and {@link javax.swing.JToggleButton}
* <strong>Value type</strong> {@link java.lang.Boolean}
*/
String SQUARE_SIZE = "JButton.squareSize";
/**
* Specifies minimum width of a component.
* <p>

View File

@@ -213,6 +213,7 @@ public class FlatButtonUI
protected void propertyChange( AbstractButton b, PropertyChangeEvent e ) {
switch( e.getPropertyName() ) {
case SQUARE_SIZE:
case MINIMUM_WIDTH:
case MINIMUM_HEIGHT:
b.revalidate();
@@ -457,13 +458,16 @@ public class FlatButtonUI
if( prefSize == null )
return null;
// make button square if it is a single-character button
// or apply minimum width, if not in toolbar and not a icon-only or single-character button
if( isIconOnlyOrSingleCharacterButton( c ) ) {
// make only single-character buttons square to allow non-square icon-only buttons
if( ((AbstractButton)c).getIcon() == null )
prefSize.width = Math.max( prefSize.width, prefSize.height );
} else if( !isToolBarButton( c ) && c.getBorder() instanceof FlatButtonBorder ) {
// make square or apply minimum width/height
boolean isIconOnlyOrSingleCharacter = isIconOnlyOrSingleCharacterButton( c );
if( clientPropertyBoolean( c, SQUARE_SIZE, false ) ) {
// make button square (increase width or height so that they are equal)
prefSize.width = prefSize.height = Math.max( prefSize.width, prefSize.height );
} else if( isIconOnlyOrSingleCharacter && ((AbstractButton)c).getIcon() == null ) {
// make single-character-no-icon button square (increase width)
prefSize.width = Math.max( prefSize.width, prefSize.height );
} else if( !isIconOnlyOrSingleCharacter && !isToolBarButton( c ) && c.getBorder() instanceof FlatButtonBorder ) {
// apply minimum width/height
float focusWidth = FlatUIUtils.getBorderFocusWidth( c );
prefSize.width = Math.max( prefSize.width, scale( FlatUIUtils.minimumWidth( c, minimumWidth ) ) + Math.round( focusWidth * 2 ) );
prefSize.height = Math.max( prefSize.height, scale( FlatUIUtils.minimumHeight( c, 0 ) ) + Math.round( focusWidth * 2 ) );