Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2005

Tip: Looking for answers? Try searching our database.

Colors for JTable cells

Thread view: 
Chanchal - 25 Oct 2005 10:45 GMT
hey,
i need to give different colors to cells of a JTable to indicate some
color coding.How is it possible

Thanks in Advance

Chanchal
Roedy Green - 25 Oct 2005 12:00 GMT
>i need to give different colors to cells of a JTable to indicate some
>color coding.How is it possible

If your problem is selecting nice colours, see
http://mindprod.com/jgloss/colour.html

If your problem is hooking them up to cells, you need to write a
TableCellRenderer and hook it up.

You really need a textbook to hold your hand through JTables.  They
are not hard, but there is so much of it, you need to go through step
by step gradually adding complications.

Here is part of one that selects foregrounds and backgrounds based on
the "dangerLevel" of a cell's row -- basically how important the row
is.

  /**
   *  Returns the component used for drawing the cell.  This method
is
   *  used to configure the renderer appropriately before drawing.
   *  Associated TableModel must implement DangerTableModel.
   *
   * @param    table           the <code>JTable</code> that is asking
the
   *                           renderer to draw; can be
<code>null</code>
   * @param    value           the value of the cell to be rendered.
It is
   *                           up to the specific renderer to
interpret
   *                           and draw the value.  For example, if
   *                           <code>value</code>
   *                           is the string "true", it could be
rendered as a
   *                           string or it could be rendered as a
check
   *                           box that is checked. <code>null</code>
is a
   *                           valid value
   * @param    isSelected      true if the cell is to be rendered
with the
   *                           selection highlighted; otherwise false
   * @param    hasFocus        if true, render cell appropriately.
For
   *                           example, put a special border on the
cell, if
   *                           the cell can be edited, render in the
color used
   *                           to indicate editing
   * @param    row             the row index of the cell being drawn.
When
   *                           drawing the header, the value of
   *                           <code>row</code> is -1
   * @param    column          the column index of the cell being
drawn
   */
  public Component getTableCellRendererComponent( JTable table,
                                                  Object value,
                                                  boolean isSelected,
                                                  boolean hasFocus,
                                                  int row,
                                                  int column)
     {

     // let original renderer handle text etc.  We just modify
colours.

     Component cell = super.getTableCellRendererComponent( table,
                                                           value,
isSelected,
                                                           hasFocus,
                                                           row,
                                                           column);

     int dangerLevel =
((DangerTableModel)(table.getModel())).getDangerLevel( row );
     // corral dangerLevel into acceptable bounds.
     if ( dangerLevel < WinAttrScheme.DANGER2 )
        {
        dangerLevel = WinAttrScheme.DANGER2;
        }
     if ( dangerLevel > WinAttrScheme.CLEAR )
        {
        dangerLevel = WinAttrScheme.CLEAR;
        }

     // normal
     cell.setForeground( winAttrScheme.getForeground( dangerLevel )
);
     cell.setBackground( winAttrScheme.getBackground( dangerLevel )
);

     cell.setFont( winAttrScheme.getFont ( dangerLevel ) );
     return cell;
     }

....
/// installing
        DangerCellRenderer renderer = new DangerCellRenderer( ... );

        TableColumnModel tm = jTable.getColumnModel();
        int columns = tm.getColumnCount();
        for ( int i=0; i<columns; i++ )
           {
           TableColumn tc = tm.getColumn(i);
           tc.setCellRenderer( renderer );
           }

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

IchBin - 25 Oct 2005 22:45 GMT
> hey,
> i need to give different colors to cells of a JTable to indicate some
[quoted text clipped - 3 lines]
>
> Chanchal

Here is an article that may help..

'Set the JTable,Choose the color of a JTable cell'
http://www.javaworld.com/javaworld/javaqa/2001-09/03-qa-0928-jtable.html

You may want to Google for others. Like..

http://groups.google.com/groups?hl=en&lr=&sa=X&oi=groupst&q=JTable+CELL+COLOR

Signature

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.