rasikaj
September 19th, 2000, 01:37 AM
Hi,
How do I change foreground color of an indivisual cell in JTable? I need to change the color depending on some condition.
I need an urgent help.
Thanks.
mjpell
September 19th, 2000, 02:11 PM
Here's how I did mine:
In the constructor of your GUI class, call
table().setDefaultRenderer(new String().getClass(), new JLabelTableCellRenderer());
package dataccesstool.gui;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.JLabel;
/**
* A specific implementation of a JLabel which can be used in a JLabel, and the JLabel's
* background color will change depending on whether or not its text is valid. This
* class is used exclusively by the MultiFileDeleteDialog.
* @author Michael Pell, Solutions Plus, Inc.
*/
public class JLabelTableCellRenderer extends JLabel implements TableCellRenderer
{
public JLabelTableCellRenderer()
{
super();
setOpaque(true); /* it´s essential */
this.setBackground(Color.white);
}
public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
String text = (String)value;
//if(text.equals("Waiting"))
//setBackground(Color.yellow); /* Decides the color else if(text.equals("Red")) * setBackground(Color.red); */
setText(text); /* Put the current text in the JLabel */
MultiFileDeleteTableModel tableModel = (MultiFileDeleteTableModel)table.getModel();
if (! tableModel.isTextValid( row, column ) )
setBackground(Color.yellow);
return this;
}
}