Skip to content

Instantly share code, notes, and snippets.

@kryogenic
Created April 24, 2015 09:25
Show Gist options
  • Save kryogenic/76148c97b30133c09767 to your computer and use it in GitHub Desktop.
Save kryogenic/76148c97b30133c09767 to your computer and use it in GitHub Desktop.
Allows you to supply an ArrayList of colors which corresponds to rows in your JList
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
/**
* Allows you to supply an ArrayList of colors which corresponds to rows in your JList
* Row 3 in the JList has the color at rowColorArrayList.get(3)
* Null color values result in the default color
*
* Usage:
* <pre>
* myJList.setCellRenderer(new ColoredListCellRenderer(rowColorArrayList));
* </pre>
*/
public class ColoredListCellRenderer extends DefaultListCellRenderer {
private final ArrayList<Color> rowColors;
public ColoredListCellRenderer(ArrayList<Color> rowColors) {
this.rowColors = rowColors;
}
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if(rowColors.size() > index) {
c.setBackground(rowColors.get(index));
}
return c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment