假设您有一个JTable
并且要为每个单元格显示三个不同颜色的字符串,例如 value1(红色)、value2(蓝色)、value3(绿色)。
我覆盖了getTableCellRendererComponent
of DefaultTableCellRenderer
butsetForeground(Color)
方法为单元格中显示的所有字符串提供了独特的颜色。
@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);
}