Skip to content

Commit

Permalink
new: support for alternate row color in JList
Browse files Browse the repository at this point in the history
  • Loading branch information
dar-dev committed Dec 13, 2024
1 parent 7bdfd49 commit c12adf1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 deletions.
13 changes: 13 additions & 0 deletions flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
* @uiDefault List.foreground Color
* @uiDefault List.selectionBackground Color
* @uiDefault List.selectionForeground Color
* @uiDefault List.alternateRowColor Color
* @uiDefault List.dropLineColor Color
* @uiDefault List.border Border
* @uiDefault List.cellRenderer ListCellRenderer
Expand Down Expand Up @@ -93,6 +94,7 @@ public class FlatListUI
@Styleable protected Color selectionForeground;
@Styleable protected Color selectionInactiveBackground;
@Styleable protected Color selectionInactiveForeground;
/** @since 3.6 */ @Styleable protected Color alternateRowColor;
/** @since 3 */ @Styleable protected Insets selectionInsets;
/** @since 3 */ @Styleable protected int selectionArc;

Expand Down Expand Up @@ -129,6 +131,7 @@ protected void installDefaults() {
selectionForeground = UIManager.getColor( "List.selectionForeground" );
selectionInactiveBackground = UIManager.getColor( "List.selectionInactiveBackground" );
selectionInactiveForeground = UIManager.getColor( "List.selectionInactiveForeground" );
alternateRowColor = UIManager.getColor( "List.alternateRowColor" );
selectionInsets = UIManager.getInsets( "List.selectionInsets" );
selectionArc = UIManager.getInt( "List.selectionArc" );

Expand All @@ -143,6 +146,7 @@ protected void uninstallDefaults() {
selectionForeground = null;
selectionInactiveBackground = null;
selectionInactiveForeground = null;
alternateRowColor = null;

oldStyleValues = null;
}
Expand Down Expand Up @@ -304,6 +308,15 @@ protected void paintCell( Graphics g, int row, Rectangle rowBounds, ListCellRend
Component rendererComponent = cellRenderer.getListCellRendererComponent( list,
dataModel.getElementAt( row ), row, isSelected,
FlatUIUtils.isPermanentFocusOwner( list ) && (row == leadIndex) );

if( alternateRowColor != null && row % 2 != 0 ) {
g.setColor( alternateRowColor );

// paint respecting selection arc
final float arc = UIScale.scale( selectionArc / 2f );
FlatUIUtils.paintSelection( (Graphics2D) g, 0, rowBounds.y, list.getWidth(), rowBounds.height,
UIScale.scale( selectionInsets ), arc, arc, arc, arc, 0 );
}

//
boolean isFileList = Boolean.TRUE.equals( list.getClientProperty( "List.isFileList" ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ void list() {
"selectionForeground", Color.class,
"selectionInactiveBackground", Color.class,
"selectionInactiveForeground", Color.class,
"alternateRowColor", Color.class,
"selectionInsets", Insets.class,
"selectionArc", int.class,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ void list() {
testColor( c, ui, "selectionForeground", 0x123456 );
testColor( c, ui, "selectionInactiveBackground", 0x123456 );
testColor( c, ui, "selectionInactiveForeground", 0x123456 );
testColor( c, ui, "alternateRowColor", 0x123456 );
testInsets( c, ui, "selectionInsets", 1,2,3,4 );
testInteger( c, ui, "selectionArc", 123 );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ void list() {
ui.applyStyle( "selectionForeground: #fff" );
ui.applyStyle( "selectionInactiveBackground: #fff" );
ui.applyStyle( "selectionInactiveForeground: #fff" );
ui.applyStyle( "alternateRowColor: #fff" );
ui.applyStyle( "selectionInsets: 1,2,3,4" );
ui.applyStyle( "selectionArc: 8" );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ List.cellNoFocusBorder
List.cellRenderer
List.dropCellBackground
List.dropCellForeground
List.alternateRowColor
List.dropLineColor
List.focusCellHighlightBorder
List.focusInputMap
Expand Down

0 comments on commit c12adf1

Please sign in to comment.