Suppose you have a JTable
and for each cell you want to display three strings with different color, say value1 (red), value2 (blue), value3 (green).
I overrode the getTableCellRendererComponent
of DefaultTableCellRenderer
but setForeground(Color)
method gives an unique color for all the string showed in the cell.
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
String s = table.getModel().getValueAt(row,column).toString();
StringTokenizer st = new StringTokenizer(s," ");
int nToken=st.countTokens();
value1=st.nextToken();
value2=st.nextToken();
value3=st.nextToken();
// so now all the values are blue...
setForeground(Color.blue);
return super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
}