From: YP <yogeshparte@HOTMAIL.COM>
Path: news.mathworks.com!newsfeed-00.mathworks.com!webcrossing
Newsgroups: comp.soft-sys.matlab
Subject: Re: Changing cell color in uitable
Message-ID: <ef5b1e4.3@webcrossing.raydaftYaTP>
Date: Wed, 20 Jun 2007 11:14:07 -0400
References: <ef5b1e4.-1@webcrossing.raydaftYaTP> <ef5b1e4.0@webcrossing.raydaftYaTP> <ef5b1e4.1@webcrossing.raydaftYaTP> <ef5b1e4.2@webcrossing.raydaftYaTP>
Lines: 270
NNTP-Posting-Host: 130.120.37.8
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
Xref: news.mathworks.com comp.soft-sys.matlab:414911



Hello Yair!
Thank you for the code.
YP
-
 Yair Altman wrote:

> You asked for it...
>
> ColoredFieldCellRenderer.java:
> ==============================
>
> // ColoredFieldCellRenderer - Modified TableCellRenderer for
> input-file fields drop-down cells
>
> // Programmed by Yair M. Altman: altmany(at)gmail.com
> // $Revision: 1.6 $ $Date: 2007/05/07 07:26:40 $
>
> import java.awt.*;
> import java.util.*;
> import javax.swing.*;
> import javax.swing.table.*;
>
> public class ColoredFieldCellRenderer extends
> DefaultTableCellRenderer implements TableCellRenderer
> {
> private Color _bgcolor = getBackground();
> private int _eventTypeLookupColumn = -1;
> private boolean _debug = false;
> private boolean _disabled = false;
> private Color _disabledColor = new Color(0.925F, 0.914F, 0.847F);
> // gray
> private Hashtable _cellBgColorHashtable = new Hashtable();
> private Hashtable _cellTooltipHashtable = new Hashtable();
>
> public Component getTableCellRendererComponent(JTable table,
> Object
> value, boolean isSelected, boolean hasFocus, int row, int column)
> {
> JComponent cell = (JComponent)
> super.getTableCellRendererComponent(table, value, isSelected,
> hasFocus, row, column);
> if (_debug) System.out.println(row + "," + column + " =>
> value:
> " + value);
>
> // Field cells may not be empty - indicate with the chosen
> bgcolor
> boolean emptyCell = false;
> String valueStr = "" + value;
> if ((value == null) || (valueStr.trim().length() == 0))
> {
> emptyCell = true;
> cell.setBackground(_bgcolor);
> } else {
> //valueStr = (String) value;
> cell.setBackground(Color.white);
> }
>
> // If this field is irrelevant to the eventType, disable it and
> indicate with a gray bgcolor
> if (_eventTypeLookupColumn >= 0)
> {
> String eventType = (String)
> table.getValueAt(row,_eventTypeLookupColumn);
> if (_debug) System.out.println(row + "," + column + " =>
> eventType: " + eventType);
> if ((eventType != null) && (eventType.equals("Target")) &&
> ((column == 6) || (column == 7)))	// "Data Field/Value" columns
> {
> cell.disable();
> cell.setBackground(_disabledColor);
> //table.setValueAt("N/A",row,6);
> if ((valueStr == null) || (!valueStr.equals("N/A")))
> {
> //setText("N/A");
> table.setValueAt("N/A",row,column);
> }
> } else {
> cell.enable();
> }
> }
> if (_disabled)
> {
> cell.disable();
> cell.setBackground(_bgcolor);
> }
>
> // If this cell should have a specific color, then use it
> Vector rowColVector = getRowColVector(row, column);
> Color cellBgColor = (Color)
> _cellBgColorHashtable.get(rowColVector);
> if (cellBgColor != null)
> cell.setBackground(cellBgColor);
>
> // If this cell is selected, then highlight with a light-blue
> color
> if (isSelected) // &&
> !(cell.getBackground().equals(Color.white)))
> {
> //cell.setBackground(cell.getBackground().darker());
> float[] rgb = cell.getBackground().getRGBComponents(null);
> rgb[0] *= .8;
> rgb[1] *= .8; // darken the R&G components only, to highlight
> the
> blue component
> cell.setBackground(new Color(rgb[0],rgb[1],rgb[2]));
> cell.setForeground(Color.black);
> }
>
> // If this cell should have a specific tooltip, then use it
> String cellTooltip = (String)
> _cellTooltipHashtable.get(rowColVector);
> if ((cellTooltip == null) || (cellTooltip.length() == 0))
> {
> // No specific tooltip set, so use the cell's string value as
> the
> tooltip
> if (value == null)
> cell.setToolTipText(null);
> else if (valueStr.length() > 200)
> {
> // Split long tooltip text into several smaller lines
> String tipText = "<html>";
> int MAX_CHARS_PER_LINE = 150;
> int strLen = valueStr.length();
> for (int lineIdx=0; lineIdx <= strLen/MAX_CHARS_PER_LINE;
> lineIdx++)
> tipText =
>
tipText.concat(valueStr.substring(lineIdx*MAX_CHARS_PER_LINE,Math.mi
> n(
> (lineIdx+1)*MAX_CHARS_PER_LINE,strLen))).concat("<br>");
> cell.setToolTipText(tipText);
> }
> else
> cell.setToolTipText(valueStr);
> }
> else
> {
> cell.setToolTipText(cellTooltip);
> }
>
> return cell;
> }
>
> public ColoredFieldCellRenderer()
> {
> this(new Color(0.925F, 0.914F, 0.847F)); // gray
> }
>
> public ColoredFieldCellRenderer(Color bgcolor)
> {
> super();
> _bgcolor = bgcolor;
> //setOpaque(false);
> setBackground(_bgcolor);
> }
>
> public ColoredFieldCellRenderer(float[] rgb)
> {
> super();
> _bgcolor = new Color(rgb[0], rgb[1], rgb[2]);
> setBackground(_bgcolor);
> }
>
> public ColoredFieldCellRenderer(float r, float g, float b)
> {
> super();
> _bgcolor = new Color(r,g,b);
> setBackground(_bgcolor);
> }
>
> public Color getBgColor()
> {
> return _bgcolor;
> }
>
> public void setBgColor(Color color)
> {
> _bgcolor = color;
> setBackground(_bgcolor);
> }
>
> public void setBgColor(float[] rgb)
> {
> _bgcolor = new Color(rgb[0], rgb[1], rgb[2]);
> setBackground(_bgcolor);
> }
>
> public void setBgColor(float r, float g, float b)
> {
> _bgcolor = new Color(r,g,b);
> setBackground(_bgcolor);
> }
>
> public void resetBgColors()
> {
> _cellBgColorHashtable.clear();
> }
>
> public void resetTooltips()
> {
> _cellTooltipHashtable.clear();
> }
>
> public void setCellBgColor(int row, int column, Color color)
> {
> Vector rowColVector = getRowColVector(row, column);
> if (color == null)
> color = Color.white; // Hashtables
> cannot
> accept nulls...
> _cellBgColorHashtable.put(rowColVector, color);
> }
>
> public void setCellTooltip(int row, int column, String text)
> {
> Vector rowColVector = getRowColVector(row, column);
> //System.out.println(row + "," + column + " => value: " +
> text);
> if (text == null)
> text = ""; // Hashtables cannot
> accept nulls...
> _cellTooltipHashtable.put(rowColVector, text);
> }
>
> public Color getCellBgColor(int row, int column)
> {
> Vector rowColVector = getRowColVector(row, column);
> return (Color) _cellBgColorHashtable.get(rowColVector);
> }
>
> public String getCellTooltip(int row, int column)
> {
> Vector rowColVector = getRowColVector(row, column);
> return (String) _cellTooltipHashtable.get(rowColVector);
> }
>
> private Vector getRowColVector(int row, int column)
> {
> Vector rowColVector = new Vector();
> rowColVector.addElement(new Integer(row));
> rowColVector.addElement(new Integer(column));
> return rowColVector;
> }
>
> public void setDebug(boolean flag)
> {
> _debug = flag;
> }
>
> public void setDisabled(boolean flag)
> {
> _disabled = flag;
> }
>
> public void setEventTypeLookupColumn(int column)
> {
> _eventTypeLookupColumn = column;
> }
> }
>
> Sample usage in Matlab:
> =======================
> warningBgColor = java.awt.Color(1,.4,.4); % light-red
> c = ColoredFieldCellRenderer(warningBgColor);
> table.getColumnModel.getColumn(1).setCellRenderer(c);
> c.setCellBgColor(row,column,warningBgColor);
>
> Yair Altman