Hi Everybody
I tried and failed to display images of employees in jTable.
I have a MySQL Table which have 4 fields - ID,Name.Phone,Photo(Blob data type). Now I am able to retrive data from this table and display it in JLabel with other data in JTextField, but How can I show the image of employee in jTable if I need a list of all 60 employees details.
MEANS I want jTable sholud show me detail like this
Till now I am able to get see the text data in jTable but not able to get the image. Here what I have done to see the image (Using Netbeans 7.1)
public class mytable
{
Connection con;
PreparedStatement psmt;
public ResultSet rs;
public void mytableFunction() {
final Object[] columnNames=new String[] {"Room No.","Room Type","Bed Type","Tariff Per Room"};
DefaultTableModel dtm=new DefaultTableModel(columnNames,0);
try
{
Class .forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee","root","");
String query = "select * from personaldetail";
psmt = con.prepareStatement(query);
rs=psmt.executeQuery();
int i = 0;
while(rs.next())
{
String var1=rs.getString(1);
String var2=rs.getString(2);
String var3=rs.getString(3);
//String var4=Integer.toString(rs.getInt(4));
dtm.addRow(new Vector());
dtm.setValueAt(var1, i, 0);
dtm.setValueAt(var2, i, 1);
dtm.setValueAt(var3, i, 2);
//dtm.setValueAt(var4, i, 3);
i++;
}
jTable1.setModel(dtm);
jTable1.getColumnModel().getColumn(3).setCellRenderer(new ImageRenderer(rs));
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
mytable mtb=new mytable();
mtb.mytableFunction();
}
class ImageRenderer extends DefaultTableCellRenderer
{
ImageIcon format;
ResultSet rs;
public ImageRenderer(ResultSet rs1)
{
rs=rs1;
}
@Override
public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected,boolean hasFocus, int row, int column)
{
JLabel label = new JLabel();
try{
byte[] imagedata=rs.getBytes(4);
format=new ImageIcon(imagedata);
if (value!=null) {
label.setHorizontalAlignment(JLabel.CENTER);
//value is parameter which filled by byteOfImage
label.setIcon(format);
}
}
catch(Exception ex){}
return label;
}
}
Thank you for any suggestion or help