DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Hybrid View

  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));




Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links