Button and ToggleButton: support square button style

This commit is contained in:
Karl Tauber
2020-01-08 13:57:42 +01:00
parent 8e84112837
commit af7c181596
10 changed files with 116 additions and 13 deletions

View File

@@ -9,6 +9,8 @@ FlatLaf Change Log
- TableHeader: Paint column borders if renderer has changed, but delegates to - TableHeader: Paint column borders if renderer has changed, but delegates to
the system default renderer (e.g. done in NetBeans). the system default renderer (e.g. done in NetBeans).
- Label and ToolTip: Fixed font sizes for HTML headings. - Label and ToolTip: Fixed font sizes for HTML headings.
- Button and ToggleButton: Support square button style (set client property
`JButton.buttonType` to `square`).
## 0.23.1 ## 0.23.1

View File

@@ -27,14 +27,25 @@ public interface FlatClientProperties
/** /**
* Specifies type of a button. * Specifies type of a button.
* <p> * <p>
* <strong>Component</strong> {@link javax.swing.JButton}<br> * <strong>Components</strong> {@link javax.swing.JButton} and {@link javax.swing.JToggleButton}<br>
* <strong>Value type</strong> {@link java.lang.String}<br> * <strong>Value type</strong> {@link java.lang.String}<br>
* <strong>Allowed Values</strong> {@link BUTTON_TYPE_HELP} * <strong>Allowed Values</strong> {@link #BUTTON_TYPE_SQUARE} and {@link #BUTTON_TYPE_HELP}
*/ */
String BUTTON_TYPE = "JButton.buttonType"; String BUTTON_TYPE = "JButton.buttonType";
/**
* Paint the button with square edges.
* <p>
* <strong>Components</strong> {@link javax.swing.JButton} and {@link javax.swing.JToggleButton}
*
* @see #BUTTON_TYPE
*/
String BUTTON_TYPE_SQUARE = "square";
/** /**
* Paint a help button (circle with question mark). * Paint a help button (circle with question mark).
* <p>
* <strong>Components</strong> {@link javax.swing.JButton}
* *
* @see #BUTTON_TYPE * @see #BUTTON_TYPE
*/ */
@@ -45,7 +56,7 @@ public interface FlatClientProperties
* <p> * <p>
* <strong>Component</strong> {@link javax.swing.JCheckBox}<br> * <strong>Component</strong> {@link javax.swing.JCheckBox}<br>
* <strong>Value type</strong> {@link java.lang.String}<br> * <strong>Value type</strong> {@link java.lang.String}<br>
* <strong>Allowed Values</strong> {@link SELECTED_STATE_INDETERMINATE} * <strong>Allowed Values</strong> {@link #SELECTED_STATE_INDETERMINATE}
*/ */
String SELECTED_STATE = "JButton.selectedState"; String SELECTED_STATE = "JButton.selectedState";

View File

@@ -75,7 +75,7 @@ public class FlatBorder
boolean isCellEditor = isTableCellEditor( c ); boolean isCellEditor = isTableCellEditor( c );
float focusWidth = isCellEditor ? 0 : getFocusWidth(); float focusWidth = isCellEditor ? 0 : getFocusWidth();
float borderWidth = getBorderWidth( c ); float borderWidth = getBorderWidth( c );
float arc = isCellEditor ? 0 : getArc(); float arc = isCellEditor ? 0 : getArc( c );
if( isFocused( c ) ) { if( isFocused( c ) ) {
g2.setColor( getFocusColor( c ) ); g2.setColor( getFocusColor( c ) );
@@ -173,7 +173,7 @@ public class FlatBorder
return getLineWidth(); return getLineWidth();
} }
protected float getArc() { protected float getArc( Component c ) {
return 0; return 0;
} }
} }

View File

@@ -110,7 +110,7 @@ public class FlatButtonBorder
} }
@Override @Override
protected float getArc() { protected float getArc( Component c ) {
return scale( (float) arc ); return FlatButtonUI.isSquareButton( c ) ? 0 : scale( (float) arc );
} }
} }

View File

@@ -209,6 +209,10 @@ public class FlatButtonUI
(icon == null && text != null && ("...".equals( text ) || text.length() == 1)); (icon == null && text != null && ("...".equals( text ) || text.length() == 1));
} }
static boolean isSquareButton( Component c ) {
return c instanceof AbstractButton && clientPropertyEquals( (AbstractButton) c, BUTTON_TYPE, BUTTON_TYPE_SQUARE );
}
static boolean isHelpButton( Component c ) { static boolean isHelpButton( Component c ) {
return c instanceof JButton && clientPropertyEquals( (JButton) c, BUTTON_TYPE, BUTTON_TYPE_HELP ); return c instanceof JButton && clientPropertyEquals( (JButton) c, BUTTON_TYPE, BUTTON_TYPE_HELP );
} }
@@ -237,7 +241,8 @@ public class FlatButtonUI
Border border = c.getBorder(); Border border = c.getBorder();
float focusWidth = (border instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0; float focusWidth = (border instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0;
float arc = (border instanceof FlatButtonBorder || isToolBarButton( c )) ? scale( (float) this.arc ) : 0; float arc = ((border instanceof FlatButtonBorder && !isSquareButton( c )) || isToolBarButton( c ))
? scale( (float) this.arc ) : 0;
boolean def = isDefaultButton( c ); boolean def = isDefaultButton( c );
// paint shadow // paint shadow

View File

@@ -17,6 +17,7 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale; import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Component;
import javax.swing.UIManager; import javax.swing.UIManager;
/** /**
@@ -32,7 +33,7 @@ public class FlatRoundBorder
protected final int arc = UIManager.getInt( "Component.arc" ); protected final int arc = UIManager.getInt( "Component.arc" );
@Override @Override
protected float getArc() { protected float getArc( Component c ) {
return scale( (float) arc ); return scale( (float) arc );
} }
} }

View File

@@ -37,6 +37,8 @@ class BasicComponentsPanel
JLabel buttonLabel = new JLabel(); JLabel buttonLabel = new JLabel();
JButton button1 = new JButton(); JButton button1 = new JButton();
JButton button2 = new JButton(); JButton button2 = new JButton();
JButton button5 = new JButton();
JButton button6 = new JButton();
JButton button3 = new JButton(); JButton button3 = new JButton();
JButton button4 = new JButton(); JButton button4 = new JButton();
JButton button13 = new JButton(); JButton button13 = new JButton();
@@ -166,10 +168,21 @@ class BasicComponentsPanel
button2.setEnabled(false); button2.setEnabled(false);
add(button2, "cell 2 1"); add(button2, "cell 2 1");
//---- button5 ----
button5.setText("square");
button5.putClientProperty("JButton.buttonType", "square");
add(button5, "cell 3 1");
//---- button6 ----
button6.setText("square");
button6.setEnabled(false);
button6.putClientProperty("JButton.buttonType", "square");
add(button6, "cell 4 1");
//---- button3 ---- //---- button3 ----
button3.setText("Help"); button3.setText("Help");
button3.putClientProperty("JButton.buttonType", "help"); button3.putClientProperty("JButton.buttonType", "help");
add(button3, "cell 3 1"); add(button3, "cell 4 1");
//---- button4 ---- //---- button4 ----
button4.setText("Help"); button4.setText("Help");

View File

@@ -1,4 +1,4 @@
JFDML JFormDesigner: "7.0.0.0.194" Java: "11.0.2" encoding: "UTF-8" JFDML JFormDesigner: "7.0.0.0.194" Java: "13.0.1" encoding: "UTF-8"
new FormModel { new FormModel {
contentType: "form/swing" contentType: "form/swing"
@@ -54,12 +54,27 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 1" "value": "cell 2 1"
} ) } )
add( new FormComponent( "javax.swing.JButton" ) {
name: "button5"
"text": "square"
"$client.JButton.buttonType": "square"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 3 1"
} )
add( new FormComponent( "javax.swing.JButton" ) {
name: "button6"
"text": "square"
"enabled": false
"$client.JButton.buttonType": "square"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 4 1"
} )
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "javax.swing.JButton" ) {
name: "button3" name: "button3"
"text": "Help" "text": "Help"
"$client.JButton.buttonType": "help" "$client.JButton.buttonType": "help"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 3 1" "value": "cell 4 1"
} ) } )
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "javax.swing.JButton" ) {
name: "button4" name: "button4"

View File

@@ -61,7 +61,9 @@ public class FlatComponentsTest
JLabel label2 = new JLabel(); JLabel label2 = new JLabel();
JLabel buttonLabel = new JLabel(); JLabel buttonLabel = new JLabel();
JButton button1 = new JButton(); JButton button1 = new JButton();
JButton button17 = new JButton();
JButton button2 = new JButton(); JButton button2 = new JButton();
JButton button18 = new JButton();
FlatComponentsTest.TestDefaultButton button5 = new FlatComponentsTest.TestDefaultButton(); FlatComponentsTest.TestDefaultButton button5 = new FlatComponentsTest.TestDefaultButton();
JButton button3 = new JButton(); JButton button3 = new JButton();
JButton button12 = new JButton(); JButton button12 = new JButton();
@@ -71,7 +73,9 @@ public class FlatComponentsTest
JButton button16 = new JButton(); JButton button16 = new JButton();
JLabel toggleButtonLabel = new JLabel(); JLabel toggleButtonLabel = new JLabel();
JToggleButton toggleButton1 = new JToggleButton(); JToggleButton toggleButton1 = new JToggleButton();
JToggleButton toggleButton9 = new JToggleButton();
JToggleButton toggleButton2 = new JToggleButton(); JToggleButton toggleButton2 = new JToggleButton();
JToggleButton toggleButton10 = new JToggleButton();
JToggleButton toggleButton3 = new JToggleButton(); JToggleButton toggleButton3 = new JToggleButton();
JToggleButton toggleButton4 = new JToggleButton(); JToggleButton toggleButton4 = new JToggleButton();
JLabel checkBoxLabel = new JLabel(); JLabel checkBoxLabel = new JLabel();
@@ -249,6 +253,11 @@ public class FlatComponentsTest
button1.setToolTipText("This button is enabled."); button1.setToolTipText("This button is enabled.");
add(button1, "cell 1 1"); add(button1, "cell 1 1");
//---- button17 ----
button17.setText("square");
button17.putClientProperty("JButton.buttonType", "square");
add(button17, "cell 1 1");
//---- button2 ---- //---- button2 ----
button2.setText("disabled"); button2.setText("disabled");
button2.setDisplayedMnemonicIndex(0); button2.setDisplayedMnemonicIndex(0);
@@ -256,6 +265,12 @@ public class FlatComponentsTest
button2.setToolTipText("This button is disabled."); button2.setToolTipText("This button is disabled.");
add(button2, "cell 2 1"); add(button2, "cell 2 1");
//---- button18 ----
button18.setText("square");
button18.putClientProperty("JButton.buttonType", "square");
button18.setEnabled(false);
add(button18, "cell 2 1");
//---- button5 ---- //---- button5 ----
button5.setText("default"); button5.setText("default");
button5.setDisplayedMnemonicIndex(0); button5.setDisplayedMnemonicIndex(0);
@@ -297,11 +312,22 @@ public class FlatComponentsTest
toggleButton1.setText("enabled"); toggleButton1.setText("enabled");
add(toggleButton1, "cell 1 2"); add(toggleButton1, "cell 1 2");
//---- toggleButton9 ----
toggleButton9.setText("square");
toggleButton9.putClientProperty("JButton.buttonType", "square");
add(toggleButton9, "cell 1 2");
//---- toggleButton2 ---- //---- toggleButton2 ----
toggleButton2.setText("disabled"); toggleButton2.setText("disabled");
toggleButton2.setEnabled(false); toggleButton2.setEnabled(false);
add(toggleButton2, "cell 2 2"); add(toggleButton2, "cell 2 2");
//---- toggleButton10 ----
toggleButton10.setText("square");
toggleButton10.putClientProperty("JButton.buttonType", "square");
toggleButton10.setEnabled(false);
add(toggleButton10, "cell 2 2");
//---- toggleButton3 ---- //---- toggleButton3 ----
toggleButton3.setText("selected"); toggleButton3.setText("selected");
toggleButton3.setSelected(true); toggleButton3.setSelected(true);

View File

@@ -47,6 +47,13 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 1" "value": "cell 1 1"
} ) } )
add( new FormComponent( "javax.swing.JButton" ) {
name: "button17"
"text": "square"
"$client.JButton.buttonType": "square"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 1"
} )
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "javax.swing.JButton" ) {
name: "button2" name: "button2"
"text": "disabled" "text": "disabled"
@@ -56,6 +63,14 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 1" "value": "cell 2 1"
} ) } )
add( new FormComponent( "javax.swing.JButton" ) {
name: "button18"
"text": "square"
"$client.JButton.buttonType": "square"
"enabled": false
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 1"
} )
add( new FormComponent( "com.formdev.flatlaf.testing.FlatComponentsTest$TestDefaultButton" ) { add( new FormComponent( "com.formdev.flatlaf.testing.FlatComponentsTest$TestDefaultButton" ) {
name: "button5" name: "button5"
"text": "default" "text": "default"
@@ -115,6 +130,13 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2" "value": "cell 1 2"
} ) } )
add( new FormComponent( "javax.swing.JToggleButton" ) {
name: "toggleButton9"
"text": "square"
"$client.JButton.buttonType": "square"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2"
} )
add( new FormComponent( "javax.swing.JToggleButton" ) { add( new FormComponent( "javax.swing.JToggleButton" ) {
name: "toggleButton2" name: "toggleButton2"
"text": "disabled" "text": "disabled"
@@ -122,6 +144,14 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 2" "value": "cell 2 2"
} ) } )
add( new FormComponent( "javax.swing.JToggleButton" ) {
name: "toggleButton10"
"text": "square"
"$client.JButton.buttonType": "square"
"enabled": false
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 2"
} )
add( new FormComponent( "javax.swing.JToggleButton" ) { add( new FormComponent( "javax.swing.JToggleButton" ) {
name: "toggleButton3" name: "toggleButton3"
"text": "selected" "text": "selected"
@@ -935,7 +965,7 @@ new FormModel {
} ) } )
}, new FormLayoutConstraints( null ) { }, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 ) "location": new java.awt.Point( 0, 0 )
"size": new java.awt.Dimension( 865, 800 ) "size": new java.awt.Dimension( 1005, 800 )
} ) } )
} }
} }