From b5dd25be5c6786098ba253e590afb6d48e306f65 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Fri, 13 Sep 2019 15:23:58 +0200 Subject: [PATCH] Table: use inactive selection background/foreground if table is not focused --- .../com/formdev/flatlaf/ui/FlatTableUI.java | 38 ++++++++++++++++++- .../com/formdev/flatlaf/FlatLaf.properties | 2 + 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java index 7757e9ae..32c1a451 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java @@ -16,8 +16,11 @@ package com.formdev.flatlaf.ui; +import java.awt.Color; +import java.awt.Graphics; import javax.swing.JComponent; import javax.swing.LookAndFeel; +import javax.swing.UIManager; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicTableUI; import com.formdev.flatlaf.util.UIScale; @@ -27,13 +30,18 @@ import com.formdev.flatlaf.util.UIScale; * * TODO document used UI defaults of superclass * - * @uiDefault Table.rowHeight int + * @uiDefault Table.rowHeight int + * @uiDefault Table.selectionInactiveBackground Color + * @uiDefault Table.selectionInactiveForeground Color * * @author Karl Tauber */ public class FlatTableUI extends BasicTableUI { + protected Color selectionInactiveBackground; + protected Color selectionInactiveForeground; + public static ComponentUI createUI( JComponent c ) { return new FlatTableUI(); } @@ -42,8 +50,36 @@ public class FlatTableUI protected void installDefaults() { super.installDefaults(); + selectionInactiveBackground = UIManager.getColor( "Tree.selectionInactiveBackground" ); + selectionInactiveForeground = UIManager.getColor( "Tree.selectionInactiveForeground" ); + int rowHeight = FlatUIUtils.getUIInt( "Table.rowHeight", 16 ); if( rowHeight > 0 ) LookAndFeel.installProperty( table, "rowHeight", UIScale.scale( rowHeight ) ); } + + @Override + protected void uninstallDefaults() { + super.uninstallDefaults(); + + selectionInactiveBackground = null; + selectionInactiveForeground = null; + } + + @Override + public void paint( Graphics g, JComponent c ) { + if( !table.hasFocus() ) { + // apply inactive selection background/foreground if table is not focused + Color oldSelectionBackground = table.getSelectionBackground(); + Color oldSelectionForeground = table.getSelectionForeground(); + table.setSelectionBackground( selectionInactiveBackground ); + table.setSelectionForeground( selectionInactiveForeground ); + + super.paint( g, c ); + + table.setSelectionBackground( oldSelectionBackground ); + table.setSelectionForeground( oldSelectionForeground ); + } else + super.paint( g, c ); + } } diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties index 2dc12421..5496fd35 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties @@ -318,6 +318,8 @@ Table.descendingSortIcon=com.formdev.flatlaf.icons.FlatDescendingSortIcon Table.sortIconColor=@icon Table.cellNoFocusBorder=2,3,2,3 Table.focusSelectedCellHighlightBorder=2,3,2,3,@cellFocusColor +Table.selectionInactiveBackground=@selectionInactiveBackground +Table.selectionInactiveForeground=@selectionInactiveForeground #---- TableHeader ----