aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Displaying a panel inside a cell of JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Displaying a panel inside a cell of JTable" Watch "Displaying a panel inside a cell of JTable" New topic
Author

Displaying a panel inside a cell of JTable

Ajit Malleri
Greenhorn

Joined: Feb 10, 2002
Posts: 27
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
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
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

Joined: Feb 10, 2002
Posts: 27
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

Joined: May 02, 2002
Posts: 196
Yes, why not. Everything is possible! TableCellRenderer is the solution. Just try it!
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
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

Joined: Feb 10, 2002
Posts: 27
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

Joined: Sep 28, 2000
Posts: 508
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying a panel inside a cell of JTable