Created
April 24, 2015 09:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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