diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java index 7f58e5de..43b3881c 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java @@ -16,7 +16,15 @@ package com.formdev.flatlaf.ui; +import java.awt.Color; +import java.awt.Component; +import java.awt.Graphics; +import java.awt.Rectangle; import javax.swing.JComponent; +import javax.swing.ListCellRenderer; +import javax.swing.ListModel; +import javax.swing.ListSelectionModel; +import javax.swing.UIManager; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicListUI; @@ -25,12 +33,64 @@ import javax.swing.plaf.basic.BasicListUI; * * TODO document used UI defaults of superclass * + * @uiDefault List.selectionInactiveBackground Color + * @uiDefault List.selectionInactiveForeground Color + * * @author Karl Tauber */ public class FlatListUI extends BasicListUI { + protected Color selectionInactiveBackground; + protected Color selectionInactiveForeground; + public static ComponentUI createUI( JComponent c ) { return new FlatListUI(); } + + @Override + protected void installDefaults() { + super.installDefaults(); + + selectionInactiveBackground = UIManager.getColor( "List.selectionInactiveBackground" ); + selectionInactiveForeground = UIManager.getColor( "List.selectionInactiveForeground" ); + } + + @Override + protected void uninstallDefaults() { + super.uninstallDefaults(); + + selectionInactiveBackground = null; + selectionInactiveForeground = null; + } + + /** + * Same as super.paintCell(), but uses inactive selection background/foreground if list is not focused. + */ + @Override + @SuppressWarnings( { "rawtypes", "unchecked" } ) + protected void paintCell( Graphics g, int row, Rectangle rowBounds, ListCellRenderer cellRenderer, + ListModel dataModel, ListSelectionModel selModel, int leadIndex ) + { + Object value = dataModel.getElementAt( row ); + boolean hasFocus = list.hasFocus(); + boolean cellHasFocus = hasFocus && (row == leadIndex); + boolean isSelected = selModel.isSelectedIndex( row ); + + // get renderer component + Component rendererComponent = cellRenderer.getListCellRendererComponent( + list, value, row, isSelected, cellHasFocus ); + + // apply inactive selection background/foreground if list is not focused + if( isSelected && !hasFocus ) { + if( rendererComponent.getBackground() == list.getSelectionBackground() ) + rendererComponent.setBackground( selectionInactiveBackground ); + if( rendererComponent.getForeground() == list.getSelectionForeground() ) + rendererComponent.setForeground( selectionInactiveForeground ); + } + + // paint renderer + rendererPane.paintComponent( g, rendererComponent, list, + rowBounds.x, rowBounds.y, rowBounds.width, rowBounds.height, true ); + } } diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties index a6e0b500..45ef756c 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties @@ -24,6 +24,8 @@ @foreground=bbbbbb @selectionBackground=4B6EAF @selectionForeground=@foreground +@selectionInactiveBackground=0D293E +@selectionInactiveForeground=@foreground @disabledText=777777 @textComponentBackground=45494A @cellFocusColor=000000 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 472d46e2..8d6cb4e7 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties @@ -165,6 +165,8 @@ List.border=1,0,1,0 List.cellNoFocusBorder=1,6,1,6 List.focusCellHighlightBorder=1,6,1,6,@cellFocusColor List.focusSelectedCellHighlightBorder=1,6,1,6,@cellFocusColor +List.selectionInactiveBackground=@selectionInactiveBackground +List.selectionInactiveForeground=@selectionInactiveForeground #---- Menu ---- diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties index fda5b5d3..13febefc 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties @@ -24,6 +24,8 @@ @foreground=000000 @selectionBackground=4A6EB7 @selectionForeground=ffffff +@selectionInactiveBackground=d4d4d4 +@selectionInactiveForeground=@foreground @disabledText=999999 @textComponentBackground=ffffff @cellFocusColor=000000 diff --git a/flatlaf-core/src/test/resources/com/formdev/flatlaf/FlatTestLaf.properties b/flatlaf-core/src/test/resources/com/formdev/flatlaf/FlatTestLaf.properties index 45eea3ca..a0c67fc1 100644 --- a/flatlaf-core/src/test/resources/com/formdev/flatlaf/FlatTestLaf.properties +++ b/flatlaf-core/src/test/resources/com/formdev/flatlaf/FlatTestLaf.properties @@ -18,6 +18,8 @@ @background=ccffcc @selectionBackground=00aa00 +@selectionInactiveBackground=888888 +@selectionInactiveForeground=ffffff @textComponentBackground=ffffff @cellFocusColor=ff0000 @icon=afafaf