Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Change the color of a JTable cell on mouse click Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Change the color of a JTable cell on mouse click" Watch "Change the color of a JTable cell on mouse click" New topic
Author

Change the color of a JTable cell on mouse click

Aji Sree
Greenhorn

Joined: Jun 19, 2009
Posts: 18
Hi,

Can anyone suggest a solution for this problem?

I have a JTable in which I want to set different fore colors for different cells on mouse click.(Please note that not for rows or columns but for individual cells).

ie initially if the fore color is black, If I click on the cell it should turned to yellow. Again if I click it should be Green and so on.

Also I want to set two or 3 different colors for the text in a particular cell (The text is separated with space and each of these text should be of different color!!)
When changing the fore colors I don't want to change the backcolor.It is by default the white color itself.

Thank you
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2180
    
    7
You need to use a custom renderer. Read the JTable API and click in the link to the Swing tutorial titled "How to Use Tables" for more information.

If you want text of different colors then you need to either:

a) use HTML in the text
b) use a JTextPane as the renderer and set color attributes for the text.

To handle the background color, you would need to add a MouseListener to the table and store a Color object for the cell to indicate the background color of the cell. The renderer would then need to query this information when setting the color of the cell.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Rob Camick wrote:To handle the background color, you would need to add a MouseListener to the table and store a Color object for the cell to indicate the background color of the cell. The renderer would then need to query this information when setting the color of the cell.


I think it should also be possible to do this entirely through cell editors, in conjunction with getValue/setValue, without using MouseListeners. So this could be an alternate approach, though probably not an easier one (especially if the cells' text is not editable by the user—if it is editable then you probably be writing cell editors either way).


bitguru blog
Aji Sree
Greenhorn

Joined: Jun 19, 2009
Posts: 18
Rob Camick wrote: You need to use a custom renderer


Thank you Rob.
Yes, I tried to use a custom renderer like this


When I run this code the entire row becomes yellow. I want only the cell which I clicked should become Yellow.
Also if I click cell on first row and then click on 4 th row the whole 4 rows(from first to fourth) becomes yellow.

Initially if the fore color is black, If I click on the cell that cell should only turned to yellow. Again if I click on it , it should be Green and so on. All other cells should retain whatever color it has.

Thanks


Aji Sree
Greenhorn

Joined: Jun 19, 2009
Posts: 18
Thank you guys.

I handled it with simple html tags and MouseListener to the table.



Thank you
Samuel March
Ranch Hand

Joined: Oct 28, 2009
Posts: 39


...did you have the fish?!............................ No.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Change the color of a JTable cell on mouse click