File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jTable setBackground Color of particular Row

 
Anand Karia
Ranch Hand
Posts: 156
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use renderer. and in the renderder class change color based on row
 
Anand Karia
Ranch Hand
Posts: 156
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this in the MyTableCellRender class --

 
Anand Karia
Ranch Hand
Posts: 156
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much for your help.

Kriti Garg and
satya kalyani
 
Megha Bhagat
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2475
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Table Row Rendering for one approach.
 
Megha Bhagat
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2475
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic