From e5761128f9138b9ef764672b2f96dafabcc91016 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Thu, 13 Feb 2020 14:51:36 +0100 Subject: [PATCH] ToggleButton: make toggle button square if it has an icon but no text or text is "..." or a single character --- CHANGELOG.md | 2 + .../formdev/flatlaf/ui/FlatButtonBorder.java | 4 +- .../com/formdev/flatlaf/ui/FlatButtonUI.java | 7 +- .../flatlaf/testing/FlatComponentsTest.java | 68 ++++++++++++++---- .../flatlaf/testing/FlatComponentsTest.jfd | 71 +++++++++++++++---- 5 files changed, 120 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca0a9119..67650abc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ FlatLaf Change Log #59, PR #62) - CheckBox and RadioButton: Do not fill background if `contentAreaFilled` is `false`. (issue #58, PR #63) +- ToggleButton: Make toggle button square if it has an icon but no text or text + is "..." or a single character. ## 0.26 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 f8ebe5a6..d9fd47b6 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 @@ -23,7 +23,7 @@ import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Insets; import java.awt.Paint; -import javax.swing.JButton; +import javax.swing.AbstractButton; import javax.swing.UIManager; import javax.swing.plaf.UIResource; @@ -98,7 +98,7 @@ public class FlatButtonBorder 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 ) + if( FlatButtonUI.isIconOnlyButton( c ) && ((AbstractButton)c).getMargin() instanceof UIResource ) insets.left = insets.right = Math.min( insets.top, insets.bottom ); return insets; 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 c8d0b2a9..558b436f 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 @@ -34,6 +34,7 @@ import javax.swing.ButtonModel; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JComponent; +import javax.swing.JToggleButton; import javax.swing.JToolBar; import javax.swing.LookAndFeel; import javax.swing.UIManager; @@ -222,11 +223,11 @@ public class FlatButtonUI } static boolean isIconOnlyButton( Component c ) { - if( !(c instanceof JButton) ) + if( !(c instanceof JButton) && !(c instanceof JToggleButton) ) return false; - Icon icon = ((JButton)c).getIcon(); - String text = ((JButton)c).getText(); + Icon icon = ((AbstractButton)c).getIcon(); + String text = ((AbstractButton)c).getText(); return (icon != null && (text == null || text.isEmpty())) || (icon == null && text != null && ("...".equals( text ) || text.length() == 1)); } 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 c27a556b..23707c25 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 @@ -87,13 +87,17 @@ public class FlatComponentsTest JToggleButton toggleButton10 = new JToggleButton(); JToggleButton toggleButton3 = new JToggleButton(); JToggleButton toggleButton4 = new JToggleButton(); - JToggleButton toggleButton5 = new JToggleButton(); - JToggleButton toggleButton8 = new JToggleButton(); + JToggleButton toggleButton11 = new JToggleButton(); + JToggleButton toggleButton12 = new JToggleButton(); + JToggleButton toggleButton13 = new JToggleButton(); + JToggleButton toggleButton14 = new JToggleButton(); JLabel checkBoxLabel = new JLabel(); JCheckBox checkBox1 = new JCheckBox(); JCheckBox checkBox2 = new JCheckBox(); JCheckBox checkBox3 = new JCheckBox(); JCheckBox checkBox4 = new JCheckBox(); + JToggleButton toggleButton5 = new JToggleButton(); + JToggleButton toggleButton8 = new JToggleButton(); JLabel radioButtonLabel = new JLabel(); JRadioButton radioButton1 = new JRadioButton(); JRadioButton radioButton2 = new JRadioButton(); @@ -203,6 +207,9 @@ public class FlatComponentsTest JButton button7 = new JButton(); JButton button8 = new JButton(); JToggleButton toggleButton6 = new JToggleButton(); + JToggleButton toggleButton15 = new JToggleButton(); + JToggleButton toggleButton16 = new JToggleButton(); + JToggleButton toggleButton17 = new JToggleButton(); //======== this ======== setLayout(new MigLayout( @@ -353,18 +360,25 @@ public class FlatComponentsTest toggleButton4.setSelected(true); add(toggleButton4, "cell 4 2"); - //---- toggleButton5 ---- - toggleButton5.setText("tab"); - toggleButton5.putClientProperty("JButton.buttonType", "tab"); - toggleButton5.setSelected(true); - add(toggleButton5, "cell 5 2"); + //---- toggleButton11 ---- + toggleButton11.setIcon(UIManager.getIcon("Tree.closedIcon")); + toggleButton11.setSelected(true); + add(toggleButton11, "cell 5 2"); - //---- toggleButton8 ---- - toggleButton8.setText("tab"); - toggleButton8.putClientProperty("JButton.buttonType", "tab"); - toggleButton8.setEnabled(false); - toggleButton8.setSelected(true); - add(toggleButton8, "cell 5 2"); + //---- toggleButton12 ---- + toggleButton12.setText("..."); + toggleButton12.setSelected(true); + add(toggleButton12, "cell 5 2"); + + //---- toggleButton13 ---- + toggleButton13.setText("\u2026"); + toggleButton13.setSelected(true); + add(toggleButton13, "cell 5 2"); + + //---- toggleButton14 ---- + toggleButton14.setText("#"); + toggleButton14.setSelected(true); + add(toggleButton14, "cell 5 2"); //---- checkBoxLabel ---- checkBoxLabel.setText("JCheckBox"); @@ -392,6 +406,19 @@ public class FlatComponentsTest checkBox4.setEnabled(false); add(checkBox4, "cell 4 3"); + //---- toggleButton5 ---- + toggleButton5.setText("tab"); + toggleButton5.putClientProperty("JButton.buttonType", "tab"); + toggleButton5.setSelected(true); + add(toggleButton5, "cell 5 3"); + + //---- toggleButton8 ---- + toggleButton8.setText("tab"); + toggleButton8.putClientProperty("JButton.buttonType", "tab"); + toggleButton8.setEnabled(false); + toggleButton8.setSelected(true); + add(toggleButton8, "cell 5 3"); + //---- radioButtonLabel ---- radioButtonLabel.setText("JRadioButton:"); add(radioButtonLabel, "cell 0 4"); @@ -979,6 +1006,21 @@ public class FlatComponentsTest toggleButton6.setIcon(UIManager.getIcon("Tree.leafIcon")); toggleButton6.setSelected(true); toolBar1.add(toggleButton6); + + //---- toggleButton15 ---- + toggleButton15.setIcon(UIManager.getIcon("FileView.computerIcon")); + toggleButton15.setSelected(true); + toolBar1.add(toggleButton15); + + //---- toggleButton16 ---- + toggleButton16.setIcon(UIManager.getIcon("FileView.floppyDriveIcon")); + toggleButton16.setSelected(true); + toolBar1.add(toggleButton16); + + //---- toggleButton17 ---- + toggleButton17.setIcon(UIManager.getIcon("FileView.hardDriveIcon")); + toggleButton17.setSelected(true); + toolBar1.add(toggleButton17); } add(toolBar1, "cell 1 23 3 1,growx"); // JFormDesigner - End of component initialization //GEN-END:initComponents 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 07d7784f..06dfcb64 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 @@ -98,7 +98,7 @@ new FormModel { } ) add( new FormComponent( "javax.swing.JButton" ) { name: "button13" - "icon": new com.jformdesigner.model.SwingIcon( 2, "Tree.closedIcon" ) + "icon": &SwingIcon0 new com.jformdesigner.model.SwingIcon( 2, "Tree.closedIcon" ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 5 1" } ) @@ -170,18 +170,29 @@ new FormModel { "value": "cell 4 2" } ) add( new FormComponent( "javax.swing.JToggleButton" ) { - name: "toggleButton5" - "text": "tab" - "$client.JButton.buttonType": "tab" + name: "toggleButton11" + "icon": #SwingIcon0 "selected": true }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 5 2" } ) add( new FormComponent( "javax.swing.JToggleButton" ) { - name: "toggleButton8" - "text": "tab" - "$client.JButton.buttonType": "tab" - "enabled": false + name: "toggleButton12" + "text": "..." + "selected": true + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 5 2" + } ) + add( new FormComponent( "javax.swing.JToggleButton" ) { + name: "toggleButton13" + "text": "…" + "selected": true + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 5 2" + } ) + add( new FormComponent( "javax.swing.JToggleButton" ) { + name: "toggleButton14" + "text": "#" "selected": true }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 5 2" @@ -222,6 +233,23 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 4 3" } ) + add( new FormComponent( "javax.swing.JToggleButton" ) { + name: "toggleButton5" + "text": "tab" + "$client.JButton.buttonType": "tab" + "selected": true + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 5 3" + } ) + add( new FormComponent( "javax.swing.JToggleButton" ) { + name: "toggleButton8" + "text": "tab" + "$client.JButton.buttonType": "tab" + "enabled": false + "selected": true + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 5 3" + } ) add( new FormComponent( "javax.swing.JLabel" ) { name: "radioButtonLabel" "text": "JRadioButton:" @@ -784,18 +812,18 @@ new FormModel { "orientation": 1 add( new FormComponent( "javax.swing.JButton" ) { name: "button9" - "icon": &SwingIcon0 new com.jformdesigner.model.SwingIcon( 2, "Tree.closedIcon" ) + "icon": &SwingIcon1 new com.jformdesigner.model.SwingIcon( 2, "Tree.closedIcon" ) } ) add( new FormComponent( "javax.swing.JButton" ) { name: "button10" - "icon": &SwingIcon1 new com.jformdesigner.model.SwingIcon( 2, "Tree.openIcon" ) + "icon": &SwingIcon2 new com.jformdesigner.model.SwingIcon( 2, "Tree.openIcon" ) } ) add( new FormComponent( "javax.swing.JToolBar$Separator" ) { name: "separator5" } ) add( new FormComponent( "javax.swing.JButton" ) { name: "button11" - "icon": &SwingIcon2 new com.jformdesigner.model.SwingIcon( 2, "Tree.leafIcon" ) + "icon": &SwingIcon3 new com.jformdesigner.model.SwingIcon( 2, "Tree.leafIcon" ) } ) add( new FormComponent( "javax.swing.JToggleButton" ) { name: "toggleButton7" @@ -963,18 +991,18 @@ new FormModel { name: "toolBar1" add( new FormComponent( "javax.swing.JButton" ) { name: "button4" - "icon": #SwingIcon0 + "icon": #SwingIcon1 } ) add( new FormComponent( "javax.swing.JButton" ) { name: "button6" - "icon": #SwingIcon1 + "icon": #SwingIcon2 } ) add( new FormComponent( "javax.swing.JToolBar$Separator" ) { name: "separator3" } ) add( new FormComponent( "javax.swing.JButton" ) { name: "button7" - "icon": #SwingIcon2 + "icon": #SwingIcon3 } ) add( new FormComponent( "javax.swing.JToolBar$Separator" ) { name: "separator4" @@ -990,6 +1018,21 @@ new FormModel { "icon": new com.jformdesigner.model.SwingIcon( 2, "Tree.leafIcon" ) "selected": true } ) + add( new FormComponent( "javax.swing.JToggleButton" ) { + name: "toggleButton15" + "icon": new com.jformdesigner.model.SwingIcon( 2, "FileView.computerIcon" ) + "selected": true + } ) + add( new FormComponent( "javax.swing.JToggleButton" ) { + name: "toggleButton16" + "icon": new com.jformdesigner.model.SwingIcon( 2, "FileView.floppyDriveIcon" ) + "selected": true + } ) + add( new FormComponent( "javax.swing.JToggleButton" ) { + name: "toggleButton17" + "icon": new com.jformdesigner.model.SwingIcon( 2, "FileView.hardDriveIcon" ) + "selected": true + } ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 1 23 3 1,growx" } )