I create class ImageRenderer that which allows me to display in JTable icon png,
i would like to add a action when i click on the icon in JTable,
ImageRenderer
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
class ImageRenderer extends DefaultTableCellRenderer {
JLabel lbl = new JLabel();
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
lbl.setIcon((ImageIcon)value);
return lbl;
}
}
and
ImageIcon icon = new ImageIcon("myicon.png");
tabla.setValueAt(icon, 0, 7);
jTable1.getColumn("Link").setCellRenderer(new ImageRenderer());
thanks
it has a simple way:
You will have to add a mouse listener to the table, and have it determine the clicked cell using
rowAtPoint()
andcolumnAtPoint()
.The components drawn by a renderer are just used as a "rubber stamp". They do not exist in the component hierarchy and so do not respond to mouse input.
and easiest for coding is implementations of
prepareRenderer
, for examplenotice prepare all Graphics Objects before as local variable, don't, never load Graphics from File, Stream not from JDBC, becuse XxxRenderer can fired its events on every, mouse, and keys events