Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Change JTable's row background dynamically

Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I need to do tricky stuff but totally lost on how and where to start. I have a JTable in my application to which I'll be adding some rows dynamically based on some events. I have used my own table model class (which extends DefaultTableModel) and also implemented a TableCellRenderer to construct the table.
Now I need to change the background colors of some rows based on some caluclated time events. To be more clear about this, once the rows are added, after sometime I need to change the color of few rows, based on some countdown timer.The color will keep on changing, as the countdown timer nears to a deadline time.
What I am planning to do is, have a seperate class to run a thread for countdown time. I'll also try to get the Jtable and the table model objects to that class. But once a certain time occurs, how do I change the colour of one particular row, and that too outside of the class contructing the table. I don't know how do I get hold of the rows I need to change the color.
Any inputs and advice will be very very helpful.

Mehul Mistry
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I solved this trick. I created a singleton class, which will register the object of my custom TableModel, I used to create the table.Also in this class I kept one method changeColor() which fires fireTableDataChanged(). My timer thread would be using this class at particular events and calls the changeColor() method.
As this is done, my table is repainted and in my TableCellRenderer I implemented the logic to change the color of the rows which I wanted. Again, which color to change and at a particular time was maintained at common place.

"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic