diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a6b91e1c..4cdedfd10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,9 @@ FlatLaf Change Log internal frames in dock. - PopupFactory: Fixed occasional `NullPointerException` in `FlatPopupFactory.fixToolTipLocation()`. (issue #305) +- Tree: Fill cell background if + `DefaultTreeCellRenderer.setBackgroundNonSelectionColor(Color)` was used. + (issue #322) - IntelliJ Themes: Fixed background colors of DesktopPane and DesktopIcon in all themes. - Native window decorations: Fixed occasional double window title bar when diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTreeUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTreeUI.java index dad7358de..6dd2c9c45 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTreeUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTreeUI.java @@ -107,6 +107,8 @@ public class FlatTreeUI protected boolean wideSelection; protected boolean showCellFocusIndicator; + private Color defaultCellNonSelectionBackground; + public static ComponentUI createUI( JComponent c ) { return new FlatTreeUI(); } @@ -125,6 +127,8 @@ protected void installDefaults() { wideSelection = UIManager.getBoolean( "Tree.wideSelection" ); showCellFocusIndicator = UIManager.getBoolean( "Tree.showCellFocusIndicator" ); + defaultCellNonSelectionBackground = UIManager.getColor( "Tree.textBackground" ); + // scale int rowHeight = FlatUIUtils.getUIInt( "Tree.rowHeight", 16 ); if( rowHeight > 0 ) @@ -144,6 +148,8 @@ protected void uninstallDefaults() { selectionInactiveBackground = null; selectionInactiveForeground = null; selectionBorderColor = null; + + defaultCellNonSelectionBackground = null; } @Override @@ -306,24 +312,24 @@ protected void paintRow( Graphics g, Rectangle clipBounds, Insets insets, Rectan } } else { // non-wide selection - int xOffset = 0; - int imageOffset = 0; - - if( rendererComponent instanceof JLabel ) { - JLabel label = (JLabel) rendererComponent; - Icon icon = label.getIcon(); - imageOffset = (icon != null && label.getText() != null) - ? icon.getIconWidth() + Math.max( label.getIconTextGap() - 1, 0 ) - : 0; - xOffset = label.getComponentOrientation().isLeftToRight() ? imageOffset : 0; - } - - g.fillRect( bounds.x + xOffset, bounds.y, bounds.width - imageOffset, bounds.height ); + paintCellBackground( g, rendererComponent, bounds ); } // this is actually not necessary because renderer should always set color // before painting, but doing anyway to avoid any side effect (in bad renderers) g.setColor( oldColor ); + } else { + // paint cell background if DefaultTreeCellRenderer.getBackgroundNonSelectionColor() is set + if( rendererComponent instanceof DefaultTreeCellRenderer ) { + DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) rendererComponent; + Color bg = renderer.getBackgroundNonSelectionColor(); + if( bg != null && !bg.equals( defaultCellNonSelectionBackground ) ) { + Color oldColor = g.getColor(); + g.setColor( bg ); + paintCellBackground( g, rendererComponent, bounds ); + g.setColor( oldColor ); + } + } } // paint renderer @@ -337,6 +343,22 @@ protected void paintRow( Graphics g, Rectangle clipBounds, Insets insets, Rectan ((DefaultTreeCellRenderer)rendererComponent).setBorderSelectionColor( oldBorderSelectionColor ); } + private void paintCellBackground( Graphics g, Component rendererComponent, Rectangle bounds ) { + int xOffset = 0; + int imageOffset = 0; + + if( rendererComponent instanceof JLabel ) { + JLabel label = (JLabel) rendererComponent; + Icon icon = label.getIcon(); + imageOffset = (icon != null && label.getText() != null) + ? icon.getIconWidth() + Math.max( label.getIconTextGap() - 1, 0 ) + : 0; + xOffset = label.getComponentOrientation().isLeftToRight() ? imageOffset : 0; + } + + g.fillRect( bounds.x + xOffset, bounds.y, bounds.width - imageOffset, bounds.height ); + } + /** * Checks whether dropping on a row. * See DefaultTreeCellRenderer.getTreeCellRendererComponent(). diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponents2Test.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponents2Test.java index 117a91a3f..3546933ce 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponents2Test.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponents2Test.java @@ -1200,10 +1200,27 @@ private static class TestDefaultTreeCellRenderer extends DefaultTreeCellRenderer { public TestDefaultTreeCellRenderer() { - setBackgroundNonSelectionColor( Color.red ); setBackgroundSelectionColor( Color.green ); setTextSelectionColor( Color.blue ); } + + @Override + public Component getTreeCellRendererComponent( JTree tree, Object value, boolean sel, boolean expanded, + boolean leaf, int row, boolean hasFocus ) + { + Color nonSelectionBg = null; + Color nonSelectionFg = null; + switch( String.valueOf( value ) ) { + case "blue": nonSelectionFg = Color.blue; break; + case "red": nonSelectionFg = Color.red; break; + case "yellow": nonSelectionBg = Color.yellow; break; + case "violet": nonSelectionBg = Color.magenta; break; + } + setBackgroundNonSelectionColor( nonSelectionBg ); + setTextNonSelectionColor( (nonSelectionFg != null) ? nonSelectionFg : UIManager.getColor( "Tree.textForeground" ) ); + + return super.getTreeCellRendererComponent( tree, value, sel, expanded, leaf, row, hasFocus ); + } } //---- class TestLabelTreeCellRenderer ------------------------------------