aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable renderer - Change the backColor of 2 specific  rows. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable renderer - Change the backColor of 2 specific  rows." Watch "JTable renderer - Change the backColor of 2 specific  rows." New topic
Author

JTable renderer - Change the backColor of 2 specific rows.

Hesham Gneady
Ranch Hand

Joined: Feb 26, 2007
Posts: 66
Hi Ranchers ,

I have a JTable with (6) columns in it, the 1st column is a (CheckBox) field.
When a user checks a checkBox of those, i want to make the backColor of the row of this checkBox=(Red) + the color of another specific row=(Blue).

I succeeded to change the color of the first row to (Red), because it's the current row i'm standing on, but how to change the color of another specific row ??

I think a renderer can render the column or cell i'm standing on right now only.


Here's a code example for what i want to do :

1. Event to check if a checkBox is checked :


2. Here's my renderer class :



Thanks
[ August 14, 2008: Message edited by: Hesham Gneady ]

Hesham
Sven Goetgeluck
Greenhorn

Joined: Jul 16, 2008
Posts: 14
hello,
try this, just addapt the casting, i dont know what your class type of the column is

Hesham Gneady
Ranch Hand

Joined: Feb 26, 2007
Posts: 66
Thanks for reply Sven.
I already tried this solution before but it didn't work. I don't know why it's not working but i think because the renderer can only change the color of the focused row. I even did another solution to check it :

After changing the color of the first row, i forced selecting row(13) and tried to change its color to (Blue), it did change its color to (Blue) but also returned back the color of the first row to (Black) ... Strange.


Here is my code :
1. Event to check if a checkBox is checked :


2. Here's my renderer class :


The behavior of the renderer is really strange, it can render the whole column or only (1) cell i'm standing on right now.
Any Suggestions ?
[ August 14, 2008: Message edited by: Hesham Gneady ]
Sven Goetgeluck
Greenhorn

Joined: Jul 16, 2008
Posts: 14
hi

you need not to do the setcellrenderer in the mouseclick event
trie to do it before when making the table and jpanel fe

sven
Hesham Gneady
Ranch Hand

Joined: Feb 26, 2007
Posts: 66
I'm not sure what you mean by this ... Do you mean that the mouse click event forces (1) row to be selected so the renderer renders only that row ?

Anyway i tried now to change the colors of a specific (2) rows just after initializing the table & filling it with the data(so not even in any user event), but it didn't work too.

Here's my code :
1. Initializing the table :


What will this actually do is that when i click with the mouse on row(5) it won't do anything, while when i click on row(7) it will make its color (BLUE).

There's something in the Renderer behavior i don't get !!
[ August 14, 2008: Message edited by: Hesham Gneady ]
Sven Goetgeluck
Greenhorn

Joined: Jul 16, 2008
Posts: 14
hi,

toke some time to work it out:

initialize table - model - renderer



some dataclass with 6 param, did some quick coding here to test it



table model who has the data and fires the table when data changes



at least renderer ( must code further when selected rows is highlited
Hesham Gneady
Ranch Hand

Joined: Feb 26, 2007
Posts: 66
Wow .... You did a great effort
Thanks a lot.

I see here you created a table, filled it with data, then assigned a renderer to its columns. Now when the user selects a row & its first cell's value is (1) then the renderer should render all this row cells to make their backColor 'RED', right ?

Now what happens when you select another row ?
Does it keep the previous selected row's color (RED) and makes the new selected row's color (RED) too ?

(Because i see my code is very similar to yours but this didn't happen to me, what happens to me is that it will update the new selected row color & resets the color of the previous selected one).
Hesham Gneady
Ranch Hand

Joined: Feb 26, 2007
Posts: 66
Thanks to Sven for help.

I've found a nice solution that will do the trick, a method called prepareRenderer() for the JTable, this method is responsible for rendering each cell in the JTable.
By overriding this method i can control the styling for each cell.

ex :


Thanks ,
Hesham
[ August 16, 2008: Message edited by: Hesham Gneady ]
Fernando Hood
Greenhorn

Joined: May 23, 2008
Posts: 20
H ithere I am a java novice but would like to se a particular row in a one column table.
If I try to see rows red I only see the last change to red.









Fernando Hood
Greenhorn

Joined: May 23, 2008
Posts: 20
This works but only if I do repaint on the frame.
It colors the rows one at a time every second.
However in netbeans I am only seeing the last result(last cell coloured).
I can't do a repaint on the frame I think in netbeans.
I am pretty close can some one nudgwe me to the finish line?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Fernando, please don't hijack other threads. You have your own thread for this: http://www.coderanch.com/t/463639/Swing-AWT-SWT-JFace/java/Jtable-retreiving-data


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable renderer - Change the backColor of 2 specific rows.