• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Displaying a panel inside a cell of JTable

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I want to display a timer inside a cell of a JTable. I have already created a timer class with the help of javax.swing.Timer. How do I incorporate the class inside the JTable
Thanks
Ajit
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to implement your own TableCellRenderer.
in the method getTableCellRendererComponent you can return your Timer. But please have in mind, that you have to handle the selection stuff (background and foreground color). If you don't handle this, it might look a bit strange.
 
Ajit Malleri
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your reply.
I have 10 columns and want to display the timer in one of the column. The row data is generated dynamically. Is it possible to display the timer in such a scenario?
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, why not. Everything is possible! TableCellRenderer is the solution. Just try it!
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the panel that is the renderer for one of the columns (your timer class) is bigger than the other cells, the row height and the row width won't be adopted to it. instead, the panel will be shrunken to fit into the cell. you have to set the row height and width accordingly.
take a look at the DefaultTableCellRenderer in 1.4: it overwrites the paint() and update(), invalidate() etc. method to improve performance - you might do that in your renderer as well.
no (mouse)events will be propagated to the components on the panel used as renderer unless you use the right TableCellEditor. I found the source of a class with these functionalities on the net, but I have no bookmark and I can't find it right now. sorry. I can paste you some code, if you need it, but I have adopted it to my needs.
Chantal
 
Ajit Malleri
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am facing some problem while executing the code. The class which is a timer class executes fine when I run it as a standalone application. But when I run it using the JTable and implementing TableCellRenderer, the values don't change. It changes only when I click on the cell. What is problem?
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that means, the table gets no notification to update itself unless you click into the cell.
check that your table model does fire the appropriate event whenever the values in the panel change. (there are events that don't trigger a table update! can't remember which ones, try it out.) tableModel.getValueAt() has to be called by the table whenever it updates cells.
chantal
reply
    Bookmark Topic Watch Topic
  • New Topic