[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


JTable - Changing a cell's background color

Posted on 2004-11-06
Medium Priority
Last Modified: 2008-03-03

I'm using a JTable and I need to, when a button is pressed, change a certain cell's color. The whole table's background color is gray and it's cells display ImageIcons. I got the Icons part up and running but I'm having great trouble on changing a cell's color - so far I've only managed to change a whole column and that's not what I need. I've been Googling for solutions for 2 hours and reading it's results for 4, so I'm getting pretty desperate here :(

For doing what I stated, what I have so far for the button's mousepressed event  is:
    DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
    jTable1.getColumnModel().getColumn(3).setCellRenderer( dtcr );

By the way, after I change the background color of a column I can't properly move an icon on to it (but that's probably because it changes the column's DefaultRenderer, I can change it back afterwards, right?). I believe, since I'm using ImageIcons on the cells I could create icons of the background color I desire and use setValueAt to change the cell's appearance -is this a solution? But anyway, I haven't gotten the icon to fully cover the cell yet...

Thanx in advance! :)

Question by:bass20

Assisted Solution

nesnemis earned 60 total points
ID: 12513671

Author Comment

ID: 12513742
Like I said, I've been Googling for a solution for 2 hours, I already had found and applied that and it didn't work or I couldn't apply it to my particular situation.
LVL 86

Assisted Solution

CEHJ earned 60 total points
ID: 12514002
>>I already had found and applied that and it didn't work

Please post that code

Author Comment

ID: 12514184
I appreciate all your help but I solved the problem using Icons - that way I don't have to go around changing DefaultTableCellRenderers, I just use setValueAt and place a colored icon on the cell I want.
If anyone has anything to add, you're welcome to do it :)
LVL 92

Accepted Solution

objects earned 105 total points
ID: 12515758
heres an example of what you need that may help:


Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month18 days, 19 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question