ToggleButton: make toggle button square if it has an icon but no text or text is "..." or a single character

This commit is contained in:
Karl Tauber
2020-02-13 14:51:36 +01:00
parent 87dd5a9ebb
commit e5761128f9
5 changed files with 120 additions and 32 deletions

View File

@@ -13,6 +13,8 @@ FlatLaf Change Log
#59, PR #62) #59, PR #62)
- CheckBox and RadioButton: Do not fill background if `contentAreaFilled` is - CheckBox and RadioButton: Do not fill background if `contentAreaFilled` is
`false`. (issue #58, PR #63) `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 ## 0.26

View File

@@ -23,7 +23,7 @@ import java.awt.GradientPaint;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Insets; import java.awt.Insets;
import java.awt.Paint; import java.awt.Paint;
import javax.swing.JButton; import javax.swing.AbstractButton;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.plaf.UIResource; import javax.swing.plaf.UIResource;
@@ -98,7 +98,7 @@ public class FlatButtonBorder
insets = super.getBorderInsets( c, insets ); insets = super.getBorderInsets( c, insets );
// use smaller left and right insets for icon-only buttons (so that they are square) // 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 ); insets.left = insets.right = Math.min( insets.top, insets.bottom );
return insets; return insets;

View File

@@ -34,6 +34,7 @@ import javax.swing.ButtonModel;
import javax.swing.Icon; import javax.swing.Icon;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JToggleButton;
import javax.swing.JToolBar; import javax.swing.JToolBar;
import javax.swing.LookAndFeel; import javax.swing.LookAndFeel;
import javax.swing.UIManager; import javax.swing.UIManager;
@@ -222,11 +223,11 @@ public class FlatButtonUI
} }
static boolean isIconOnlyButton( Component c ) { static boolean isIconOnlyButton( Component c ) {
if( !(c instanceof JButton) ) if( !(c instanceof JButton) && !(c instanceof JToggleButton) )
return false; return false;
Icon icon = ((JButton)c).getIcon(); Icon icon = ((AbstractButton)c).getIcon();
String text = ((JButton)c).getText(); String text = ((AbstractButton)c).getText();
return (icon != null && (text == null || text.isEmpty())) || return (icon != null && (text == null || text.isEmpty())) ||
(icon == null && text != null && ("...".equals( text ) || text.length() == 1)); (icon == null && text != null && ("...".equals( text ) || text.length() == 1));
} }

View File

@@ -87,13 +87,17 @@ public class FlatComponentsTest
JToggleButton toggleButton10 = new JToggleButton(); JToggleButton toggleButton10 = new JToggleButton();
JToggleButton toggleButton3 = new JToggleButton(); JToggleButton toggleButton3 = new JToggleButton();
JToggleButton toggleButton4 = new JToggleButton(); JToggleButton toggleButton4 = new JToggleButton();
JToggleButton toggleButton5 = new JToggleButton(); JToggleButton toggleButton11 = new JToggleButton();
JToggleButton toggleButton8 = new JToggleButton(); JToggleButton toggleButton12 = new JToggleButton();
JToggleButton toggleButton13 = new JToggleButton();
JToggleButton toggleButton14 = new JToggleButton();
JLabel checkBoxLabel = new JLabel(); JLabel checkBoxLabel = new JLabel();
JCheckBox checkBox1 = new JCheckBox(); JCheckBox checkBox1 = new JCheckBox();
JCheckBox checkBox2 = new JCheckBox(); JCheckBox checkBox2 = new JCheckBox();
JCheckBox checkBox3 = new JCheckBox(); JCheckBox checkBox3 = new JCheckBox();
JCheckBox checkBox4 = new JCheckBox(); JCheckBox checkBox4 = new JCheckBox();
JToggleButton toggleButton5 = new JToggleButton();
JToggleButton toggleButton8 = new JToggleButton();
JLabel radioButtonLabel = new JLabel(); JLabel radioButtonLabel = new JLabel();
JRadioButton radioButton1 = new JRadioButton(); JRadioButton radioButton1 = new JRadioButton();
JRadioButton radioButton2 = new JRadioButton(); JRadioButton radioButton2 = new JRadioButton();
@@ -203,6 +207,9 @@ public class FlatComponentsTest
JButton button7 = new JButton(); JButton button7 = new JButton();
JButton button8 = new JButton(); JButton button8 = new JButton();
JToggleButton toggleButton6 = new JToggleButton(); JToggleButton toggleButton6 = new JToggleButton();
JToggleButton toggleButton15 = new JToggleButton();
JToggleButton toggleButton16 = new JToggleButton();
JToggleButton toggleButton17 = new JToggleButton();
//======== this ======== //======== this ========
setLayout(new MigLayout( setLayout(new MigLayout(
@@ -353,18 +360,25 @@ public class FlatComponentsTest
toggleButton4.setSelected(true); toggleButton4.setSelected(true);
add(toggleButton4, "cell 4 2"); add(toggleButton4, "cell 4 2");
//---- toggleButton5 ---- //---- toggleButton11 ----
toggleButton5.setText("tab"); toggleButton11.setIcon(UIManager.getIcon("Tree.closedIcon"));
toggleButton5.putClientProperty("JButton.buttonType", "tab"); toggleButton11.setSelected(true);
toggleButton5.setSelected(true); add(toggleButton11, "cell 5 2");
add(toggleButton5, "cell 5 2");
//---- toggleButton8 ---- //---- toggleButton12 ----
toggleButton8.setText("tab"); toggleButton12.setText("...");
toggleButton8.putClientProperty("JButton.buttonType", "tab"); toggleButton12.setSelected(true);
toggleButton8.setEnabled(false); add(toggleButton12, "cell 5 2");
toggleButton8.setSelected(true);
add(toggleButton8, "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 ----
checkBoxLabel.setText("JCheckBox"); checkBoxLabel.setText("JCheckBox");
@@ -392,6 +406,19 @@ public class FlatComponentsTest
checkBox4.setEnabled(false); checkBox4.setEnabled(false);
add(checkBox4, "cell 4 3"); 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 ----
radioButtonLabel.setText("JRadioButton:"); radioButtonLabel.setText("JRadioButton:");
add(radioButtonLabel, "cell 0 4"); add(radioButtonLabel, "cell 0 4");
@@ -979,6 +1006,21 @@ public class FlatComponentsTest
toggleButton6.setIcon(UIManager.getIcon("Tree.leafIcon")); toggleButton6.setIcon(UIManager.getIcon("Tree.leafIcon"));
toggleButton6.setSelected(true); toggleButton6.setSelected(true);
toolBar1.add(toggleButton6); 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"); add(toolBar1, "cell 1 23 3 1,growx");
// JFormDesigner - End of component initialization //GEN-END:initComponents // JFormDesigner - End of component initialization //GEN-END:initComponents

View File

@@ -98,7 +98,7 @@ new FormModel {
} ) } )
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "javax.swing.JButton" ) {
name: "button13" 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 ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 5 1" "value": "cell 5 1"
} ) } )
@@ -170,18 +170,29 @@ new FormModel {
"value": "cell 4 2" "value": "cell 4 2"
} ) } )
add( new FormComponent( "javax.swing.JToggleButton" ) { add( new FormComponent( "javax.swing.JToggleButton" ) {
name: "toggleButton5" name: "toggleButton11"
"text": "tab" "icon": #SwingIcon0
"$client.JButton.buttonType": "tab"
"selected": true "selected": true
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 5 2" "value": "cell 5 2"
} ) } )
add( new FormComponent( "javax.swing.JToggleButton" ) { add( new FormComponent( "javax.swing.JToggleButton" ) {
name: "toggleButton8" name: "toggleButton12"
"text": "tab" "text": "..."
"$client.JButton.buttonType": "tab" "selected": true
"enabled": false }, 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 "selected": true
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 5 2" "value": "cell 5 2"
@@ -222,6 +233,23 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 4 3" "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" ) { add( new FormComponent( "javax.swing.JLabel" ) {
name: "radioButtonLabel" name: "radioButtonLabel"
"text": "JRadioButton:" "text": "JRadioButton:"
@@ -784,18 +812,18 @@ new FormModel {
"orientation": 1 "orientation": 1
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "javax.swing.JButton" ) {
name: "button9" 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" ) { add( new FormComponent( "javax.swing.JButton" ) {
name: "button10" 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" ) { add( new FormComponent( "javax.swing.JToolBar$Separator" ) {
name: "separator5" name: "separator5"
} ) } )
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "javax.swing.JButton" ) {
name: "button11" 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" ) { add( new FormComponent( "javax.swing.JToggleButton" ) {
name: "toggleButton7" name: "toggleButton7"
@@ -963,18 +991,18 @@ new FormModel {
name: "toolBar1" name: "toolBar1"
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "javax.swing.JButton" ) {
name: "button4" name: "button4"
"icon": #SwingIcon0 "icon": #SwingIcon1
} ) } )
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "javax.swing.JButton" ) {
name: "button6" name: "button6"
"icon": #SwingIcon1 "icon": #SwingIcon2
} ) } )
add( new FormComponent( "javax.swing.JToolBar$Separator" ) { add( new FormComponent( "javax.swing.JToolBar$Separator" ) {
name: "separator3" name: "separator3"
} ) } )
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "javax.swing.JButton" ) {
name: "button7" name: "button7"
"icon": #SwingIcon2 "icon": #SwingIcon3
} ) } )
add( new FormComponent( "javax.swing.JToolBar$Separator" ) { add( new FormComponent( "javax.swing.JToolBar$Separator" ) {
name: "separator4" name: "separator4"
@@ -990,6 +1018,21 @@ new FormModel {
"icon": new com.jformdesigner.model.SwingIcon( 2, "Tree.leafIcon" ) "icon": new com.jformdesigner.model.SwingIcon( 2, "Tree.leafIcon" )
"selected": true "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 ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 23 3 1,growx" "value": "cell 1 23 3 1,growx"
} ) } )