Skip to content

Commit

Permalink
Button and ToggleButton: ToolBar buttons now respect explicitly set b…
Browse files Browse the repository at this point in the history
…ackground color. If no background color is set, then the button background is not painted anymore (issue #191)
  • Loading branch information
DevCharly committed Jan 13, 2021
1 parent 8d14d5f commit b49a498
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,13 @@ protected Color getBackground( JComponent c ) {
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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ private void initComponents() {
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();
Expand Down Expand Up @@ -411,7 +411,7 @@ private void initComponents() {
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();
Expand All @@ -421,13 +421,13 @@ private void initComponents() {
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();
Expand Down Expand Up @@ -1684,4 +1684,37 @@ public void updateUI() {
}
}
}

//---- 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit b49a498

Please sign in to comment.