From 1c52f1f76ce2444d7529a61c27fea0c3f490df66 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Thu, 22 Apr 2021 00:14:42 +0200 Subject: [PATCH] CheckBox and RadioButton: do not fill background if used as cell renderer, except if cell is selected or has different background color (issue #311) --- CHANGELOG.md | 4 +++- .../formdev/flatlaf/ui/FlatRadioButtonUI.java | 20 +++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06d492a7..b20aee12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRadioButtonUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRadioButtonUI.java index 14344717..56450014 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRadioButtonUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRadioButtonUI.java @@ -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)