I want to be able to have a JPanel in a cell with a JButton that does some work when clicked.
I looked for howtos about Cell Editors, but all examples talk about replacing the cell with another component (e.g. replace an int with a JTextField etc.) My situation is a little different:
I have the following ADT
class MyClass {
public String title;
public String url;
public String path;
public int annotations;
}
I created a custom table cell model that has 1 column and the class for that column is MyClass. Then I created a cell renderer for that class that returns a JPanel as shown here:

As you can see, the JPanel contains a button. I want this button to launch a JFrame whenever clicked. Any ideas?
If you will suggest Cell Editor, please be a little more specific about how to do so. If possible, provide some pseudo-code.
Thank you.
P.S. I’m pretty sure the title of this question needs some work. 😉
After coding.mof’s reply, I finally did what I wanted. However, I wanted a more complete answer for this question, so I will provide one myself.
So, Cell Renderers simply draw the component and do not allow any interactions within it. While Cell Editors do.
Initially, all cells in the JTable are components that are returned by the registered renderer. However, when a cell is selected, this component is replaced by a component that is returned by the editor. These two can actually be different components! Which I’m pretty sure you can take advantage of this and make some funky cells 😛
Anyway, in this example, both the renderer and the editor display the same component, so we will create a component that will be used by both.
First, we need to create a TableModel that returns our ADT:
Now, we create a component that will be shared between the Renderer and the Editor:
The isSelected and table parameters are used to render the background of the panel and are optional. Here is how the renderer uses our component:
And here is how the editor uses it:
Thats all. Now we can simply create a JTable as follows:
And we’re done!
P.S. I’m pretty sure that we can combine the Renderer and the Editor into a single class the extends AbstractCellEditor and implements TableCellRenderer, but I’m not sure about performance.