Skip to content

Commit

Permalink
new: support for alternate row color in jtree
Browse files Browse the repository at this point in the history
  • Loading branch information
dar-dev committed Oct 22, 2024
1 parent c29a276 commit 33e37a7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
* @uiDefault Tree.selectionForeground Color
* @uiDefault Tree.selectionInactiveBackground Color
* @uiDefault Tree.selectionInactiveForeground Color
* @uiDefault Tree.alternateRowColor Color
* @uiDefault Tree.selectionInsets Insets
* @uiDefault Tree.selectionArc int
* @uiDefault Tree.wideSelection boolean
Expand Down Expand Up @@ -141,6 +142,7 @@ public class FlatTreeUI
@Styleable protected Color selectionInactiveBackground;
@Styleable protected Color selectionInactiveForeground;
@Styleable protected Color selectionBorderColor;
@Styleable protected Color alternateRowColor;
/** @since 3 */ @Styleable protected Insets selectionInsets;
/** @since 3 */ @Styleable protected int selectionArc;
@Styleable protected boolean wideSelection;
Expand Down Expand Up @@ -192,6 +194,7 @@ protected void installDefaults() {
selectionInactiveBackground = UIManager.getColor( "Tree.selectionInactiveBackground" );
selectionInactiveForeground = UIManager.getColor( "Tree.selectionInactiveForeground" );
selectionBorderColor = UIManager.getColor( "Tree.selectionBorderColor" );
alternateRowColor = UIManager.getColor( "Tree.alternateRowColor" );
selectionInsets = UIManager.getInsets( "Tree.selectionInsets" );
selectionArc = UIManager.getInt( "Tree.selectionArc" );
wideSelection = UIManager.getBoolean( "Tree.wideSelection" );
Expand Down Expand Up @@ -227,6 +230,7 @@ protected void uninstallDefaults() {
selectionInactiveBackground = null;
selectionInactiveForeground = null;
selectionBorderColor = null;
alternateRowColor = null;

defaultLeafIcon = null;
defaultClosedIcon = null;
Expand Down Expand Up @@ -570,6 +574,11 @@ protected void paintRow( Graphics g, Rectangle clipBounds, Insets insets, Rectan
boolean isSelected = tree.isRowSelected( row );
boolean isDropRow = isDropRow( row );
boolean needsSelectionPainting = (isSelected || isDropRow) && isPaintSelection();

if( alternateRowColor != null && row % 2 != 0 ) {
g.setColor( alternateRowColor );
FlatUIUtils.paintComponentBackground((Graphics2D) g, bounds.x, bounds.y, tree.getWidth(), bounds.height, 0, 0);
}

// do not paint row if editing
if( isEditing ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,7 @@ void tree() {
"selectionInactiveBackground", Color.class,
"selectionInactiveForeground", Color.class,
"selectionBorderColor", Color.class,
"alternateRowColor", Color.class,
"selectionInsets", Insets.class,
"selectionArc", int.class,
"wideSelection", boolean.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,7 @@ Tree.dropCellForeground
Tree.dropLineColor
Tree.editorBorder
Tree.editorBorderSelectionColor
Tree.alternateRowColor
Tree.expandedIcon
Tree.focusInputMap
Tree.focusInputMap.RightToLeft
Expand Down

0 comments on commit 33e37a7

Please sign in to comment.