aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes jTable setBackground Color of particular Row Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "jTable setBackground Color of particular Row" Watch "jTable setBackground Color of particular Row" New topic
Author

jTable setBackground Color of particular Row

Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

Hi All

Can anyone tell me how to change the background color of a particular row.

Let i have 100 rows and i just want to change the background color of 10 rows randomly having column 1 value = 1.

if (col1 == 1)
{
//wht should be the code.
}

can anyone help me to solve this problem.


Love is GOD and GOD is Love.
Anand Karia
IT Concretor.......
M/s. Anand Karia Concreting IT
Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
use renderer. and in the renderder class change color based on row


Author of JPhotoBrush Pro (www.jphotobrushpro.com)
Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

I tried my all effort to solve this problem but could not get through.

I just want to change the color of column # 3 and based on recordset change the color of rows.

I got an example but it colors all data into grid that has only results.




Kriti Garg
Ranch Hand

Joined: Sep 13, 2004
Posts: 50
Hi,

Try something like this

........................

if (Ur Condition Met) {
setBackground(isSelected ? table.getSelectionBackground():Color.RED);
setForeground(isSelected ? table.getSelectionForeground():Color.WHITE);

}
else {
setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
setForeground(isSelected ? table.getSelectionForeground() :
table.getForeground());

}

where table is ur JTable
Bbye
satya kalyani
Greenhorn

Joined: Jan 07, 2005
Posts: 2
try this in the MyTableCellRender class --

Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

thank you very much for your help.

Kriti Garg and
satya kalyani
Megha Bhagat
Greenhorn

Joined: Mar 14, 2013
Posts: 3
Hello friends,

I am having same problem as above.
And I have tried solution Satya Kalyani has given.
But the problem is that I am getting row rendering for one row only.
Please help me... how to solve it..
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
See Table Row Rendering for one approach.
Megha Bhagat
Greenhorn

Joined: Mar 14, 2013
Posts: 3
Thank you Rob Camick for your suggestion but I am not getting answer from that link also.

Actually I want Row color red when it is having value less than 40 in any column.
And the code which I have shown only colors first row only as first row having value less than 40 but other rows which are having value less than 40 are having background white only.
And I started loop from 1 position because my first column contains String value and other column contains Numeric value.

I am showing my code here may anyone can suggest me that why it is not working properly.

My Renderer file is having code as below.


So please give me suggestion so that I get rendering for each row.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
So please give me suggestion so that I get rendering for each row.


I did give you a suggestion which you completely ignored. Nowhere in my suggestion did I suggest using a custom renderer. In fact I advised against using a custom renderer.

The code I gave you checks a single value and then highlights the entire row. All you need to do is loop through all values in the row to determine the background color.
Megha Bhagat
Greenhorn

Joined: Mar 14, 2013
Posts: 3
I did give you a suggestion which you completely ignored. Nowhere in my suggestion did I suggest using a custom renderer. In fact I advised against using a custom renderer.


Actually I am only allowed to use Custom Rendering. So please suggest me on the basis of that criteria.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: jTable setBackground Color of particular Row