Button: default button

This commit is contained in:
Karl Tauber
2019-08-20 12:21:22 +02:00
parent 2ef440a016
commit f2f4a451ac
7 changed files with 74 additions and 18 deletions

View File

@@ -51,10 +51,11 @@ public class FlatButtonBorder
}
}
public Paint getBorderColor( Component c ) {
private Paint getBorderColor( Component c ) {
if( c.isEnabled() ) {
Color startColor = UIManager.getColor( "Button.startBorderColor" );
Color endColor = UIManager.getColor( "Button.endBorderColor" );
boolean def = FlatButtonUI.isDefaultButton( c );
Color startColor = UIManager.getColor( def ? "Button.default.startBorderColor" : "Button.startBorderColor" );
Color endColor = UIManager.getColor( def ? "Button.default.endBorderColor" : "Button.endBorderColor" );
return (startColor.equals( endColor ) )
? startColor
: new GradientPaint( 0, getFocusWidth(), startColor,

View File

@@ -16,11 +16,14 @@
package com.formdev.flatlaf.ui;
import java.awt.Color;
import java.awt.Component;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
@@ -43,6 +46,10 @@ public class FlatButtonUI
return instance;
}
static boolean isDefaultButton( Component c ) {
return c instanceof JButton && ((JButton)c).isDefaultButton();
}
@Override
public void update( Graphics g, JComponent c ) {
if( c.isOpaque() ) {
@@ -57,7 +64,7 @@ public class FlatButtonUI
float focusWidth = 2;
float arc = 6;
g2.setColor( c.getBackground() );
g2.setColor( getBackground( c ) );
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, arc );
} finally {
g2.dispose();
@@ -71,16 +78,22 @@ public class FlatButtonUI
@Override
protected void paintText( Graphics g, JComponent c, Rectangle textRect, String text ) {
AbstractButton b = (AbstractButton) c;
if( b.getModel().isEnabled() )
super.paintText( g, c, textRect, text );
else {
// paint disabled text
FontMetrics fm = SwingUtilities2.getFontMetrics( c, g );
int mnemonicIndex = b.getDisplayedMnemonicIndex();
g.setColor( UIManager.getColor( "Button.disabledText" ) );
SwingUtilities2.drawStringUnderlineCharAt( c, g, text, mnemonicIndex,
textRect.x + getTextShiftOffset(),
textRect.y + fm.getAscent() + getTextShiftOffset() );
}
FontMetrics fm = SwingUtilities2.getFontMetrics( c, g );
int mnemonicIndex = b.getDisplayedMnemonicIndex();
g.setColor( b.getModel().isEnabled() ? getForeground( c ) : UIManager.getColor( "Button.disabledText" ) );
SwingUtilities2.drawStringUnderlineCharAt( c, g, text, mnemonicIndex,
textRect.x + getTextShiftOffset(),
textRect.y + fm.getAscent() + getTextShiftOffset() );
}
private Color getBackground( Component c ) {
boolean def = FlatButtonUI.isDefaultButton( c );
return def ? UIManager.getColor( "Button.default.background" ) : c.getBackground();
}
private Color getForeground( Component c ) {
boolean def = FlatButtonUI.isDefaultButton( c );
return def ? UIManager.getColor( "Button.default.foreground" ) : c.getForeground();
}
}

View File

@@ -51,6 +51,11 @@ Button.startBorderColor=5e6060
Button.endBorderColor=5e6060
Button.disabledBorderColor=5e6060
Button.default.background=365880
Button.default.foreground=bbbbbb
Button.default.startBorderColor=4c708c
Button.default.endBorderColor=4c708c
#---- Label ----

View File

@@ -51,6 +51,11 @@ Button.startBorderColor=bfbfbf
Button.endBorderColor=b3b3b3
Button.disabledBorderColor=cfcfcf
Button.default.background=4A86C7
Button.default.foreground=f0f0f0
Button.default.startBorderColor=4779ba
Button.default.endBorderColor=3167ad
#---- Label ----

View File

@@ -55,12 +55,13 @@ public class FlatComponentsTest
JButton button2 = new JButton();
JButton button3 = new JButton();
JButton button4 = new JButton();
FlatComponentsTest.TestDefaultButton button5 = new FlatComponentsTest.TestDefaultButton();
//======== this ========
setLayout(new GridBagLayout());
((GridBagLayout)getLayout()).columnWidths = new int[] {0, 0, 0, 0, 0, 0};
((GridBagLayout)getLayout()).columnWidths = new int[] {0, 0, 0, 0, 0, 0, 0};
((GridBagLayout)getLayout()).rowHeights = new int[] {0, 0, 0};
((GridBagLayout)getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
((GridBagLayout)getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
((GridBagLayout)getLayout()).rowWeights = new double[] {0.0, 0.0, 1.0E-4};
//---- labelLabel ----
@@ -117,6 +118,13 @@ public class FlatComponentsTest
button4.setSelected(true);
button4.setEnabled(false);
add(button4, new GridBagConstraints(4, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 5), 0, 0));
//---- button5 ----
button5.setText("default");
button5.setDisplayedMnemonicIndex(0);
add(button5, new GridBagConstraints(5, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
// JFormDesigner - End of component initialization //GEN-END:initComponents
@@ -124,4 +132,15 @@ public class FlatComponentsTest
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
// JFormDesigner - End of variables declaration //GEN-END:variables
//---- class TestDefaultButton --------------------------------------------
private static class TestDefaultButton
extends JButton
{
@Override
public boolean isDefaultButton() {
return true;
}
}
}

View File

@@ -7,7 +7,7 @@ new FormModel {
"JavaCodeGenerator.defaultVariableLocal": true
}
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class java.awt.GridBagLayout ) {
"$columnSpecs": "0, 0, 0, 0, 0"
"$columnSpecs": "0, 0, 0, 0, 0, 0"
"$rowSpecs": "0, 0"
"$hGap": 5
"$vGap": 5
@@ -74,6 +74,14 @@ new FormModel {
"gridx": 4
"gridy": 1
} )
add( new FormComponent( "com.formdev.flatlaf.FlatComponentsTest$TestDefaultButton" ) {
name: "button5"
"text": "default"
"displayedMnemonicIndex": 0
}, new FormLayoutConstraints( class com.jformdesigner.runtime.GridBagConstraintsEx ) {
"gridx": 5
"gridy": 1
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 )
"size": new java.awt.Dimension( 580, 300 )

View File

@@ -29,6 +29,11 @@ Button.startBorderColor=ff0000
Button.endBorderColor=0000ff
Button.disabledBorderColor=000088
Button.default.background=dddddd
Button.default.foreground=880000
Button.default.startBorderColor=ff0000
Button.default.endBorderColor=ff0000
#---- Label ----