The values in the list contain either COMPLETED, or INCOMPLETE, and I would like to change their colour according to their value.
First off there's no need to keep these three lists:
ArrayList<Object> values = new ArrayList<>();
ArrayList<String> complete = new ArrayList<>();
ArrayList<String> incomplete = new ArrayList<>();
The ListCellRenderer component is evaluated for each element in the JList so these lists are pointless. You can implement your requirement easily doing as follows:
private class MyCellRender extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if(value.toString().contains("INCOMPLETE") || value.toString().contains("COMPLETED")) {
Color fg = value.toString().contains("COMPLETED") ? Color.green : Color.red;
setForeground(fg);
} else {
setForeground(list.getForeground());
}
return this;
}
}
Take a look to Writing a Custom Cell Renderer section in How to Use Lists trail for further details.