I am making a system where the user can save a picture and database. It is already saving, but I want that saved photo to be displayed on a JLabel when I click it from the JTable. I don't know the code on how make it appear, and I want it to be automatically resized according to the size of my JLabel.
Here is the code for the Table Mouse Clicked Event:
private void tblTableMouseClicked(java.awt.event.MouseEvent evt) {
int row = tblTable.getSelectedRow();
String selection = tblTable.getModel().getValueAt(row, 0).toString();
String sql = "select * from LibrarySystemDatabase where No = " + selection;
try {
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if (rs.next()) {
txtNo.setText(rs.getString("No"));
txtTitle.setText(rs.getString("Title"));
txtAuthor.setText(rs.getString("Author"));
txtGenre.setText(rs.getString("Genre"));
txtLexile.setText(rs.getString("Lexile"));
txtPoints.setText(rs.getString("Points"));
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch (Exception e) {
}
}
}
Here is the code for the Upload Button:
private void btnUploadActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
filename = f.getAbsolutePath();
ImageIcon imageIcon = new ImageIcon (new ImageIcon(filename).getImage().getScaledInstance(lblImage.getWidth(), lblImage.getHeight(), Image.SCALE_DEFAULT));
lblImage.setIcon(imageIcon);
try {
File image = new File(filename);
FileInputStream fix = new FileInputStream(image);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for (int number; (number = fix.read(buf)) != -1;) {
bos.write(buf, 0, number);
}
bookImage = bos.toByteArray();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
//*/
}
byte[] bookImage = null;
String filename = null;
private ImageIcon format = null;
Thanks!