Results 1 to 7 of 7
  1. #1
    johnt is offline Super Moderator
    Join Date
    Apr 2007
    Posts
    30
    Rep Power
    0

    Default How to change color of JTable row having a particular value

    I am trying to change the color of entire row which is having a column of value "FAIL". Here is my code, which is changing only that particular column. But i want to change the entire row.

    Can anyone please give me an idea?

    Java Code:
    public Component getTableCellRendererComponent(
    JTable table, Object value, boolean isSelected, boolean hasFocus,int row,int col)
    {
    
        String s = table.getModel().getValueAt(row,col).toString();
    
        if(s.equalsIgnoreCase("Fail")) {
             setForeground(Color.red);
        }else {
             setForeground(null);
        }
        
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, 
    row, col);
    
    }

  2. #2
    levent Guest

    Default

    If you know the column in the model that the test value will reside in, set it as a constant and your could would end up looking like this.

    Java Code:
    public Component getTableCellRendererComponent(  
    JTable table, Object value, boolean isSelected, 
    boolean hasFocus, int row, int col)
    {
         Component comp = super.getTableCellRendererComponent(
                          table,  value, isSelected, hasFocus, row, col);
    
         String s =  table.getModel().getValueAt(row, VALIDATION_COLUMN ).toString();
    
         if(s.equalsIgnoreCase("Fail")) 
         {
             comp.setForeground(Color.red);
         }
         else 
         {
             comp.setForeground(null);
         }
    
         return( comp );
     }

  3. #3
    johnt is offline Super Moderator
    Join Date
    Apr 2007
    Posts
    30
    Rep Power
    0

    Default

    Thanks, it is working.

  4. #4
    dhnsekaran is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default

    me also its amazing

  5. #5
    kkk
    kkk is offline Member
    Join Date
    Jul 2009
    Posts
    11
    Rep Power
    0

    Default Thanks

    Thanks for your. It helps me at correct time.:)

  6. #6
    anumis15 is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default Still Unable To Do It

    Hello To All,
    I m fresher in java my requirement is same and my code is look like this.

    package mytestapplication;
    import java.awt.Color;
    import java.awt.Component;
    import javax.swing.JTable;
    import javax.swing.table.TableCellRenderer;
    public class Renderer implements TableCellRenderer {
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    //throw new UnsupportedOperationException("Not supported yet.");
    Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    String s = table.getModel().getValueAt(row, column).toString();
    if (something) {
    comp.setForeground(Color.red);
    } else {
    comp.setForeground(null);
    }
    return (comp);
    }
    }


    it is giving an error in line "Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);" that can not find symbol for this method what to do?



    Plz Reply Urgetly..............

  7. #7
    lumartineru is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Talking Conditional colouring a date column

    I'm not Swing expert but found this solution to colouring a date column if already passed (I have two dates in my table, and I need to colour the one in the 5th column).

    I'm sure it can be improved but that really helped me was to consider that rows can be sorted and then you need to convert the row view index to the row model index otherwise you got a NullPointerException.

    Java Code:
        public class MyTableCellRenderer extends DefaultTableCellRenderer {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                                                           boolean hasFocus, int row, int column){
                if (value instanceof String && column == 5) {
                    try {
                        row = table.convertRowIndexToModel(row);
                        String s = table.getModel().getValueAt(row, column).toString();
                        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm");
                        Date date = (Date) formatter.parse(s);
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(date);
    
                        if (cal.before(Calendar.getInstance())) {
                            setForeground(Color.RED);
                        } else {
                            setForeground(null);
                        }
                        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    
                    } catch (ParseException e) {
                        System.out.println("Exception :" + e);
                        return null;
                    }
                } else {
                    setForeground(null);
                    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                }
            }
        }
    Hope this helps.
    Luis
    Last edited by lumartineru; 05-14-2011 at 07:09 AM.

Similar Threads

  1. Replies: 5
    Last Post: 04-25-2009, 08:00 PM
  2. Change the color in my program
    By carl in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 12:20 PM
  3. A bit of color!
    By tim in forum Java 2D
    Replies: 8
    Last Post: 02-11-2008, 11:57 PM
  4. How to change TXT color Onclick
    By dave700800 in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 01:39 AM
  5. Color of the focued row in a JTable
    By SteM in forum AWT / Swing
    Replies: 2
    Last Post: 11-20-2007, 06:55 PM

Posting Permissions

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