CheckBox and RadioButton: do not fill background if used as cell renderer, except if cell is selected or has different background color (issue #311)

This commit is contained in:
Karl Tauber
2021-04-22 00:14:42 +02:00
parent 9bd3a68115
commit 1c52f1f76c
2 changed files with 21 additions and 3 deletions

View File

@@ -26,6 +26,8 @@ FlatLaf Change Log
#### Fixed bugs
- CheckBox and RadioButton: Do not fill background if used as cell renderer,
except if cell is selected or has different background color. (issue #311)
- DesktopPane:
- Fixed missing preview of iconified internal frames in dock when using a
custom desktop manager. (PR #294)
@@ -202,7 +204,7 @@ FlatLaf Change Log
- CheckBox and RadioButton: Fill component background as soon as background
color is different to default background color, even if component is not
opaque (which is the default). This paints selection if using the component as
cell renderer a Table, Tree or List.
cell renderer in Table, Tree or List.
- TextComponents: Border of focused non-editable text components had wrong
color.
- Custom window decorations: Fixed top window border in dark themes when running

View File

@@ -18,11 +18,14 @@ package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.util.Objects;
import javax.swing.AbstractButton;
import javax.swing.CellRendererPane;
import javax.swing.JComponent;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
@@ -120,10 +123,11 @@ public class FlatRadioButtonUI
public void paint( Graphics g, JComponent c ) {
// fill background even if not opaque if
// - contentAreaFilled is true and
// - if background was explicitly set to a non-UIResource color
// - if background color is different to default background color
// (this paints selection if using the component as cell renderer)
if( !c.isOpaque() &&
((AbstractButton)c).isContentAreaFilled() &&
!defaultBackground.equals( c.getBackground() ) )
!Objects.equals( c.getBackground(), getDefaultBackground( c ) ) )
{
g.setColor( c.getBackground() );
g.fillRect( 0, 0, c.getWidth(), c.getHeight() );
@@ -160,6 +164,18 @@ public class FlatRadioButtonUI
FlatButtonUI.paintText( g, b, textRect, text, b.isEnabled() ? b.getForeground() : disabledText );
}
/**
* Returns the default background color of the component.
* If the component is used as cell renderer (e.g. in JTable),
* then the background color of the renderer container is returned.
*/
private Color getDefaultBackground( JComponent c ) {
Container parent = c.getParent();
return (parent instanceof CellRendererPane && parent.getParent() != null)
? parent.getParent().getBackground()
: defaultBackground;
}
private int getIconFocusWidth( JComponent c ) {
AbstractButton b = (AbstractButton) c;
return (b.getIcon() == null && getDefaultIcon() instanceof FlatCheckBoxIcon)