|
"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <ef5b1e4.2@webcrossing.raydaftYaTP>...
> YP wrote:
> >
> > 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
>
>
> 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.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
Greetings,
Yair's contribution to matlabbers is undeniable. I would like to ask whether anyone used the ColoredFieldCellRenderer.java.
I did compile the java code and got the class and then attempted to use the given matlab code to change the background color, but however, i think i am doing sth wrong:
dat = {6.125, 456.3457, true, 'Fixed';...
6.75, 510.2342, false, 'Adjustable';...
7, 658.2, false, 'Fixed';};
columnname = {'Rate', 'Amount', 'Available', 'Fixed/Adj'};
columnformat = {'numeric', 'bank', 'logical', {'Fixed' 'Adjustable'}};
columneditable = [false false true true];
t = uitable('Units','normalized','Position',...
[0.1 0.1 0.9 0.9], 'Data', dat,...
'ColumnName', columnname,...
'ColumnFormat', columnformat,...
'ColumnEditable', columneditable,...
'RowName',[]);
jscroll = findjobj(t);
%jscroll(2) is the uiscrollpane which is what we want i think
table = jscroll(2).getViewport.getComponent(0);
warningBgColor = java.awt.Color(1,.4,.4); % light-red
c = ColoredFieldCellRenderer(warningBgColor);
table.getColumnModel.getColumn(1).setCellRenderer(c);
c.setCellBgColor(1,1,warningBgColor);
However, nothing seems to happen
Thanks
Can
|