This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
The easy way is have your TableModel return ImageIcon.class or Icon.class for that column. That will render the cell's value as an image. Next, add a MouseListener to the entire table. Inside its methods, you can call the JTable's columnAtPoint and rowAtPoint methods to find out which cell was clicked. If the column matches then perform the action:
You don't need a custom cell renderer. If your table model returns one of those classes, then JTable will use one of its own internal cell renders to display the icon.
So your table model will have to override getColumnClass:
You can also choose other classes; Number.class and its subclasses will handle numeric conversion as well as right-aligning the number. Boolean.class will use a check box for both rendering and editing. And as said before, ImageIcon.class and Icon.class will use an image.
Please note that the value must be castable to the class, or you will get ClassCastExceptions.
Joined: May 01, 2008
How to add MouseListener to table
addMouseListener(java.awt.event.MouseListener) in java.awt.Component cannot be applied to (table.TableIcon)
I got this error, when I am trying to add MouseListener