Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everybody :)

I want to change the background color of specific table cell in java JTable.........

given raw number and column number???

i tried that but i can only change the background of selected cell by mouse using mouse listener.

like this

<br />
table.setSelectionBackground(Color.red);<br />


but i want to change the table cell according to its raw and column numbers

if anyone know how can i do that please help me as soon as possible ?


thanks in advance.

Basem Moghram,
Posted
Updated 30-Dec-09 22:26pm
v2

Since I gave you that bogus answer, I thought I'd look into this for you, even though I don't know Java very well.

Looks like there is no easy way to do this. You apparently have to create a custom TableCellRenderer. This article describes how to do this in detail.
 
Share this answer
 
Thanks aspdotnetdev for ur answer


but i already have the raw and column numbers

and i want to change the background of this cell only.

how can i do that ????

Thanks .

Basem,
 
Share this answer
 
Thanks aspdotnetdev very much , :)

i have seen that link before and the problem still found since this code change the background of the whole table's cell :(


Thanks,
 
Share this answer
 
What do you mean by "problem still found since this code change the background of the whole table's cell"? I thought you only wanted to change the background color of an individual cell? I downloaded and ran the code from the webpage I sent to you and it made the backgrounds of only certain cells red. Isn't that what you want?
 
Share this answer
 
Thanks aspdotnetdev for ur effort ,

simply i have to implement the code of Connect 4 game
and i use the table to implement the game board and i need to represent the 2 players in 2 different color since when any player hits any column with the mouse i change the empty cell with the player color according to the game rules

this what i want in simple way?? :)

since i can change the color of the selected cell by the mouse as i told u before since i get only the column number when the player select the column by the mouse and the row number i get it according to the game rules.

now i have the column number and the row number so i want to change the background color with the player color and so on.


Thanks Again and And best Regards,


Basem.
 
Share this answer
 
v3
In theory, the children of the table would be the table rows (TR). You could do a for loop over those until you reached the desired row. The children of the rows would probably be the TD's (the columns). You could loop over those until you reach the specified column. Then you could set the style properties on that TD.

Alternatively, you could give each TD an ID (e.g., "R1C1", "R1C2", "R2C1", etc.) then just use getElementById to find the TD at that row/column.

EDIT: I assumed you meant JavaScript, but I see you have tagged this as Java. If I was incorrect, go ahead and vote my answer a 1 and I'll remove it.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900