JTable - Changing a cell's background color

Posted on 2004-11-06
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
    LVL 3

    Assisted Solution

    LVL 1

    Author Comment

    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

    >>I already had found and applied that and it didn't work

    Please post that code
    LVL 1

    Author Comment

    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

    heres an example of what you need that may help:

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    twoTwo  challenge 35 72
    FizzBuzz challenge 9 65
    scoresIncreasing challenge 10 48
    parentbit challenge 3 37
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now