0

I have a table in my project called tasktable. It retrives data from database (oracle). How can I change the color of the row that has the color in the cell ex. (i,8) automatically when I click the refresh button?

I have tried so many times to put that source code on specific row, but it ended up coloring all the table:

          int count;      
          count = tasktable.getRowCount();
         for (int i=0;i<count;i++)
            { 
                  rr = new Object ();
                   rr = tasktable.getModel().getValueAt(i,8);
                   if(rr.equals("GREEN"))
                   {
                 setBackground(Color.GREEN);
                   }
                    if(rr.equals("red"))
                   {
                       setBackground(Color.red);
                   }
                     if(rr.equals("BLUE"))
                   {
                      setBackground(Color.BLUE);
                   }
                      if(rr.equals("yellow"))
                   {
                     setBackground(Color.yellow);
                   }
                       if(rr.equals("pink"))
                   {
                     setBackground(Color.pink);
                   }
                       if(rr.equals(null))
                   {
                     setBackground(null);
                   }

how can help me out upon this problem ?

Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114
Odette Savage
  • 17
  • 1
  • 4
  • 2
    You really should do some research before asking. This is a pretty common and easy thing to do. http://stackoverflow.com/a/3550006/933756 – Quinma Jul 16 '12 at 15:59
  • possible duplicate of [Change the background color of a row in a JTable](http://stackoverflow.com/questions/3875607/change-the-background-color-of-a-row-in-a-jtable) – Guillaume Polet Jul 16 '12 at 17:37
  • You might want to read through http://docs.oracle.com/javase/tutorial/uiswing/components/table.html – MadProgrammer Jul 16 '12 at 18:15
  • possible duplicate of [How do I set the JTable column and row color?](http://stackoverflow.com/questions/3548986/how-do-i-set-the-jtable-column-and-row-color) – Joe Dec 21 '14 at 01:58

4 Answers4

2

Add a custom TableCellRenderer to your table.

Rob Wagner
  • 4,391
  • 15
  • 24
1

setBackground() sets the background color of the JTable, not the background color of each row or cell. You need a TableCellRenderer, as @Recursed said.

Dan O
  • 6,022
  • 2
  • 32
  • 50
1

If all you're doing is changing row color, subclass your JTable and override the prepareRenderer method:

public Component prepareRenderer(TableCellRenderer renderer,
                             int row,
                             int column) {    
     Component c = super.prepareRenderer(renderer, row, column);
     if (row == HIGHLIGHT_ROW) {
          c.setBackground(BG_COLOR); 
     }
     return c;
}
BenCole
  • 2,092
  • 3
  • 17
  • 26
  • i try to write your code but it show to me this error cannot find symbol symbol: method prepareRenderer(javax.swing.table.TableCellRenderer,int,int) – Odette Savage Jul 17 '12 at 15:01
  • Hmm...I'd need more info to tell you more about what's wrong. What's the full error message? What is the code you're trying to run? – BenCole Jul 17 '12 at 15:29
0
 Component comp = super.getTableCellRendererComponent(
                  table,  value, isSelected, hasFocus, row, col);

 String s =  table.getModel().getValueAt(row, VALIDATION_COLUMN ).toString();
 comp.setForeground(Color.red);

http://www.java-forums.org/awt-swing/541-how-change-color-jtable-row-having-particular-value.html

Sully
  • 14,672
  • 5
  • 54
  • 79