CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2003
    Posts
    18

    changing the color of a row in a JTable

    how do i change the color of a row in a JTable..Wat's the source code to use?... Anybody knows....?

    Thanks alot...

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    This is asked quite often here. If you do a search of this forum for something like "JTable row color change", you should find plenty of information.

    Time wounds all heels
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Jan 2003
    Posts
    18
    hi..i did a search of the forum..i found many similar queries regarding the changing of colors of rows in a JTable but i still do not understand..I know i have to use the TableCellRenderer but i'm not so familiar...Could u help me out again? Thanks alot..

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    OK, here's an example off the top of my head:
    Code:
    // Create a renderer to color a row
    class MyTableCellRenderer extends DefaultTableCellRenderer { 
       public Component getTableCellRendererComponent(JTable table, 
                         Object value,
                         boolean isSelected,
                         boolean hasFocus,
                         int row,
                         int column) { 
       // let the superclass do all the work
       Component renderer = super.getTableCellRendererComponent(
                      value, isSelected, hasFocus, row, column);
       // Change background to the required color 
       // if this is the required row
       if (row == requiredRow) {
          renderer.setBackground(requiredColor);
       }
       return renderer;
    }
    
    ...
    
    // Set this renderer for all class types (i.e. columns) in the table
    myTable.setDefaultRenderer( Object.class, new MyTableCellRenderer());
    This is untested code, but it shows the basic technique.

    The higher, the fewer...
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured