Title: RE: FW: I need some help regarding JTable

>Hi there,
>   I am trying to create JTable which when passed color and row-index changes
>the color of the specified row( specified by rowIndex)
>  It is very easy for evry column to setRenderer but can we set for every
>row?I tried to do that but it is not working properly.Can you pls help me in
>this!

that is because you can get the renderer for the whole collumn.  in JTables, the columns are more tightly defined.  Un like that, all the cells in a row don't get globed together.  This allows you to add rows easily, but adding columns at run time is a hack.  However, from this you loose on the other side with rows as to not be able to set the renderer for a whole row.

> Subclass JTable, if you haven't already.  In the subclass, override
> getCellRenderer(int,int) to return a new TableCellRenderer that does
> what you want.
> Now, you can set the color on the renderer as you've done before,
> and the table should change appearance everywhere.  (You might
> have to call repaint() on the table.)

you will have to subclass the defaulttablerenderer too.  This is what I did in my renderer subclassing with a JList:

  public Component getListCellRendererComponent (JList list, Object value, int index, boolean isSelected, boolean hasFocus)

  {
    if (value == null)
      super.getListCellRendererComponent(list, value, index, isSelected, hasFocus);
    setComponentOrientation(list.getComponentOrientation());
    if (frame.isDead(value.toString().substring(0, value.toString().indexOf(" "))))
    {
      if (isSelected)
      {
        setBackground(Color.red);
        setForeground(Color.white);
      }
      else
      {
        setBackground(list.getBackground());
        setForeground(Color.red);
      }
      Font font = list.getFont();
      font = new Font(font.getName(), font.ITALIC | font.BOLD, font.getSize());
      setFont(font);
    }
    else
      if (isSelected)
      {
        setBackground(frame.getBackgroundHightlight(value.toString().substring(0, value.toString().indexOf(" "))));            

        setForeground(frame.getForegroundHightlight(value.toString().substring(0, value.toString().indexOf(" "))));           

        setFont(list.getFont());
      }
      else
      {
        UIManager.put("ToolTip.background", Color.yellow);
        UIManager.put("ToolTip.foreground", Color.black);
        setBackground(list.getBackground());
        setForeground(list.getForeground());
        setFont(list.getFont());
      }
    setText((value == null) ? "" : value.toString());
    setEnabled(list.isEnabled());
    setBorder((hasFocus) ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
    return  this;
  }

the component object passed in here can be of your subclassed JTable, then you could call methods from your subc JTable that asks which color to paint the renderer's objects.

remember, a renderer is just a JLabel in disguise.
rev aaron
ps. I'm sending this wednesday feb 7th at 3:30PM GMT could some people tell me when they get this?  My e-mails to the list have been taking days to go through...

Reply via email to