Home > Archive > Matlab > June 2007 > Changing cell color in uitable
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Changing cell color in uitable
|
|
|
| Hello!
Is it possible to change background color of the particular cell in
uitable? If so how? Specifically I want to color the cell using user
provided RGB values.
I am using MATLAB 7.0.4.365 (R14) Service Pack 2 and Java 1.5.0 with
Sun Microsystems Inc. Java HotSpot(TM) Client VM
(mixed mode)
I no very little Java and swing programming. I saw other posts
related to coloring columns but then coloring cell was not very
obvious.
with regards,
YP
---
| |
| Yair Altman 2007-06-19, 8:14 am |
| YP wrote:
>
> Hello!
> Is it possible to change background color of the particular cell in
> uitable? If so how? Specifically I want to color the cell using
> user provided RGB values.
> I am using MATLAB 7.0.4.365 (R14) Service Pack 2 and Java 1.5.0
> with Sun Microsystems Inc. Java HotSpot(TM) Client VM
> (mixed mode)
>
> I no very little Java and swing programming. I saw other posts
> related to coloring columns but then coloring cell was not very
> obvious.
>
> with regards,
> YP
Unfortunately, if you want to color specific cells based on content
or some other logic, you need to define a user-class in Java. It's
not difficult, but you need to know Java for this.
Yair Altman
| |
|
| Hello!
Is it possible to provide me with a code snippet?
with regards,
YP
----
Yair Altman wrote:
> Unfortunately, if you want to color specific cells based on content
> or some other logic, you need to define a user-class in Java. It's
> not difficult, but you need to know Java for this.
>
> Yair Altman
| |
| Yair Altman 2007-06-20, 4:25 am |
| YP wrote:[color=darkred]
>
> Hello!
> Is it possible to provide me with a code snippet?
> with regards,
> YP
> ----
>
> Yair Altman wrote:
> content or some other logic, you need to define a user-class in
> Java. It's not difficult, but you need to know Java for this.
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,Mat
h.min(
(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
| |
|
| 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,Mat
h.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
|
|
|
|
|