Button and ToggleButton: ToolBar buttons now respect explicitly set background color. If no background color is set, then the button background is not painted anymore (issue #191)

This commit is contained in:
Karl Tauber
2021-01-13 17:22:09 +01:00
parent 8d14d5f87c
commit b49a498f9c
4 changed files with 51 additions and 10 deletions

View File

@@ -27,6 +27,9 @@ FlatLaf Change Log
- Button and ToggleButton: Threat Unicode surrogate character pair as single
character and make button square. (issue #234)
- Button and ToggleButton: ToolBar buttons now respect explicitly set background
color. If no background color is set, then the button background is not
painted anymore. (issue #191)
- TabbedPane: Fixed `IndexOutOfBoundsException` when using tooltip text on close
buttons and closing last/rightmost tab. (issue #235)
- Extras: Added missing export of package

View File

@@ -410,8 +410,13 @@ public class FlatButtonUI
if( model.isRollover() )
return toolbarHoverBackground;
// use background of toolbar
return c.getParent().getBackground();
// use component background if explicitly set
Color bg = c.getBackground();
if( isCustomBackground( bg ) )
return bg;
// do not paint background
return null;
}
boolean def = isDefaultButton( c );

View File

@@ -356,7 +356,7 @@ public class FlatComponentsTest
JScrollPane scrollPane14 = new JScrollPane();
progressBar3 = new FlatProgressBar();
progressBar4 = new FlatProgressBar();
JToolBar toolBar2 = new JToolBar();
FlatComponentsTest.TestToolBar toolBar2 = new FlatComponentsTest.TestToolBar();
JButton button9 = new JButton();
JButton button10 = new JButton();
JButton button11 = new JButton();
@@ -411,7 +411,7 @@ public class FlatComponentsTest
JToolTip toolTip1 = new JToolTip();
JToolTip toolTip2 = new JToolTip();
JLabel toolBarLabel = new JLabel();
JToolBar toolBar1 = new JToolBar();
FlatComponentsTest.TestToolBar toolBar1 = new FlatComponentsTest.TestToolBar();
JButton button4 = new JButton();
JButton button6 = new JButton();
JButton button7 = new JButton();
@@ -421,13 +421,13 @@ public class FlatComponentsTest
JToggleButton toggleButton16 = new JToggleButton();
JToggleButton toggleButton17 = new JToggleButton();
JLabel label3 = new JLabel();
JToolBar toolBar3 = new JToolBar();
FlatComponentsTest.TestToolBar toolBar3 = new FlatComponentsTest.TestToolBar();
FlatButton button26 = new FlatButton();
FlatButton button27 = new FlatButton();
FlatToggleButton toggleButton23 = new FlatToggleButton();
FlatToggleButton toggleButton24 = new FlatToggleButton();
JLabel label4 = new JLabel();
JToolBar toolBar4 = new JToolBar();
FlatComponentsTest.TestToolBar toolBar4 = new FlatComponentsTest.TestToolBar();
FlatButton button28 = new FlatButton();
FlatButton button29 = new FlatButton();
FlatToggleButton toggleButton25 = new FlatToggleButton();
@@ -1684,4 +1684,37 @@ public class FlatComponentsTest
}
}
}
//---- class TestToolBar --------------------------------------------------
private static class TestToolBar
extends JToolBar
{
@Override
protected void paintComponent( Graphics g ) {
super.paintComponent( g );
if( isPaintBackgroundPattern() && isOpaque() ) {
int width = getWidth();
int height = getHeight();
g.setColor( Color.blue );
for( int y = 0; y < height; y += 2 )
g.drawLine( 0, y, width - 1, y );
}
}
/**
* Overridden to see which components paint background with color from parent.
*/
@Override
public Color getBackground() {
return isPaintBackgroundPattern() ? Color.orange : super.getBackground();
}
private boolean isPaintBackgroundPattern() {
FlatTestFrame frame = (FlatTestFrame) SwingUtilities.getAncestorOfClass( FlatTestFrame.class, this );
return frame != null && frame.isPaintBackgroundPattern();
}
}
}

View File

@@ -933,7 +933,7 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 4 13 1 6,growy"
} )
add( new FormContainer( "javax.swing.JToolBar", new FormLayoutManager( class javax.swing.JToolBar ) ) {
add( new FormContainer( "com.formdev.flatlaf.testing.FlatComponentsTest$TestToolBar", new FormLayoutManager( class javax.swing.JToolBar ) ) {
name: "toolBar2"
"orientation": 1
add( new FormComponent( "javax.swing.JButton" ) {
@@ -1391,7 +1391,7 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 23"
} )
add( new FormContainer( "javax.swing.JToolBar", new FormLayoutManager( class javax.swing.JToolBar ) ) {
add( new FormContainer( "com.formdev.flatlaf.testing.FlatComponentsTest$TestToolBar", new FormLayoutManager( class javax.swing.JToolBar ) ) {
name: "toolBar1"
add( new FormComponent( "javax.swing.JButton" ) {
name: "button4"
@@ -1446,7 +1446,7 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 23 5 1"
} )
add( new FormContainer( "javax.swing.JToolBar", new FormLayoutManager( class javax.swing.JToolBar ) ) {
add( new FormContainer( "com.formdev.flatlaf.testing.FlatComponentsTest$TestToolBar", new FormLayoutManager( class javax.swing.JToolBar ) ) {
name: "toolBar3"
add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) {
name: "button26"
@@ -1479,7 +1479,7 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 23 5 1"
} )
add( new FormContainer( "javax.swing.JToolBar", new FormLayoutManager( class javax.swing.JToolBar ) ) {
add( new FormContainer( "com.formdev.flatlaf.testing.FlatComponentsTest$TestToolBar", new FormLayoutManager( class javax.swing.JToolBar ) ) {
name: "toolBar4"
add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) {
name: "button28"