I have a JTable created using this code :
table = new JTable(new DefaultTableModel(
new Object[][] {
},
en_entete
)
)
{
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
URL fileURL = null;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/test", "root", "root");
PreparedStatement ps = conn.prepareStatement("select name, img from table where id=1");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String name = rs.getString("name");
File temp = File.createTempFile(name, ".jpg");
Blob blob = rs.getBlob("img");
int blobLength = (int) blob.length();
byte[] bytes = blob.getBytes(1, blobLength);
blob.free();
BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));
ImageIO.write(img, "jpg", temp);
fileURL = temp.toURI().toURL();
}
} catch (Exception ex) {
ex.printStackTrace();
}
html
= "<html><body>"
+ "<img src='"
+ fileURL + "'>";
Component c = super.prepareRenderer(renderer, row, column);
if (c instanceof JComponent) {
JComponent jc = (JComponent) c;
jc.setToolTipText(html);
}
return c;
}
};
So when the mouse hovers over a cell of my JTable, the image show up. But I want that image to be different for each cell (changing the SQL request) and to be shown only for the first column.
Thanks in advance