Table: use inactive selection background/foreground if table is not focused

This commit is contained in:
Karl Tauber
2019-09-13 15:23:58 +02:00
parent 995a71e87b
commit b5dd25be5c
2 changed files with 39 additions and 1 deletions

View File

@@ -16,8 +16,11 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.LookAndFeel; import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTableUI; import javax.swing.plaf.basic.BasicTableUI;
import com.formdev.flatlaf.util.UIScale; import com.formdev.flatlaf.util.UIScale;
@@ -27,13 +30,18 @@ import com.formdev.flatlaf.util.UIScale;
* *
* TODO document used UI defaults of superclass * 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 * @author Karl Tauber
*/ */
public class FlatTableUI public class FlatTableUI
extends BasicTableUI extends BasicTableUI
{ {
protected Color selectionInactiveBackground;
protected Color selectionInactiveForeground;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
return new FlatTableUI(); return new FlatTableUI();
} }
@@ -42,8 +50,36 @@ public class FlatTableUI
protected void installDefaults() { protected void installDefaults() {
super.installDefaults(); super.installDefaults();
selectionInactiveBackground = UIManager.getColor( "Tree.selectionInactiveBackground" );
selectionInactiveForeground = UIManager.getColor( "Tree.selectionInactiveForeground" );
int rowHeight = FlatUIUtils.getUIInt( "Table.rowHeight", 16 ); int rowHeight = FlatUIUtils.getUIInt( "Table.rowHeight", 16 );
if( rowHeight > 0 ) if( rowHeight > 0 )
LookAndFeel.installProperty( table, "rowHeight", UIScale.scale( rowHeight ) ); 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 );
}
} }

View File

@@ -318,6 +318,8 @@ Table.descendingSortIcon=com.formdev.flatlaf.icons.FlatDescendingSortIcon
Table.sortIconColor=@icon Table.sortIconColor=@icon
Table.cellNoFocusBorder=2,3,2,3 Table.cellNoFocusBorder=2,3,2,3
Table.focusSelectedCellHighlightBorder=2,3,2,3,@cellFocusColor Table.focusSelectedCellHighlightBorder=2,3,2,3,@cellFocusColor
Table.selectionInactiveBackground=@selectionInactiveBackground
Table.selectionInactiveForeground=@selectionInactiveForeground
#---- TableHeader ---- #---- TableHeader ----