JTable: colored rows, how to?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: JTable: colored rows, how to?

  1. #1
    Fabio Luis De Paoli Guest

    JTable: colored rows, how to?

    I've searched the JTable API and the Java tutorial but I haven't been able
    to discover how to change the color of one specific JTable row.

    I have an application with a table that lists monetary values and I want to
    color with red the rows that have a currency different from the apps'
    standard currency.

    Another variation that would suit my needs would be to make the row use a
    bold or italic font or a different font from the standard JTable font.

    Tks, Fabio


  2. #2
    Paul Clapham Guest

    Re: JTable: colored rows, how to?


    Fabio Luis De Paoli <fpaoli@br.dhl.com> wrote in message
    news:3A3BB419.9050902@br.dhl.com...
    > I've searched the JTable API and the Java tutorial but I haven't been able
    > to discover how to change the color of one specific JTable row.
    >

    You use a JTableRenderer to do that. Here's an example of one that I use:

    public class IStringTableRenderer extends BNBTableRenderer implements
    TableCellRenderer
    {
    private IString rendered;

    public IStringTableRenderer(IString is) {
    rendered = is;
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus,
    int row, int col) {
    Component renderer = super.getTableCellRendererComponent(table, value,
    isSelected, hasFocus, row, col);
    if (row != 0 && rendered.isOverridden(row))
    renderer.setFont(BNBUtil.boldFont);
    else
    renderer.setFont(BNBUtil.plainFont);
    return renderer;
    }
    }

    To have the JTable use this to render its cells, I just write

    theTable.setDefaultRenderer(Object.class, new IStringTableRenderer(is));




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center