Click to See Complete Forum and Search --> : Table Cell


yoosly
February 20th, 2000, 08:22 AM
Hi,

I have the code as below:

import javax.swing.table.DefaultTableCellRenderer;
import java.text.DecimalFormat;
import java.awt.*;
import javax.swing.JTable;


public class ColoredTableCellRenderer extends DefaultTableCellRenderer{

DecimalFormat formatter=new DecimalFormat("#0.000");

public ColoredTableCellRenderer() {
}

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row,int column) {
Component renderer = super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);

if (isSelected){
renderer.setBackground(Color.black);
if (column!=4)
renderer.setForeground(Color.white); renderer.setFont(new Font("Dialog", Font.ITALIC, 11));
}
else{
if (row==0)
renderer.setBackground(new Color(240, 250, 210));
else if (1 == row % 2)
renderer.setBackground(new Color(210, 250, 210));
else
renderer.setBackground(new Color(240, 250, 210));
if (column!=4)
renderer.setForeground(Color.black);
renderer.setFont(new Font("Dialog", Font.PLAIN, 11));
}
return renderer;
}

public void setValue(Object value){

if (value instanceof ColorData) {
ColorData cvalue = (ColorData)value;
setForeground(cvalue.cellForeColor);
setText((cvalue.cellData==null)?"0.000"
: formatter.format(cvalue.cellData));
}
else if (value instanceof Double){
setText((value == null) ? "0.000"
: formatter.format(value));
}

else{
super.setValue(value);
}
}
}

*********************
What should I do if I want to change the background color of that cell if the value is changed?

Thanks in advance.

yoosly
February 20th, 2000, 08:52 AM
For more info, we do not know the value in the cell. So that, I can't fit the value for every cell as the example code I read b4. so, anyway to change the background cell when the value to be set is different with the value showing b4 and then changed back the previous background color of the cell?? I need to do this to alert the user.

Thanks very much for ur infomation.