I'm pulling my hair out on this one. Many questions like this here on SO, but I can't get it to work.
I'm trying to add an image to an existing JPanel. The problem is getting the image to be visible in the JPanel. The code runs, but the image is nowhere..
Here's my code:
private void loadImgBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile();
BufferedImage myPicture = null;
try {
myPicture = ImageIO.read(file);
} catch (IOException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
imagePnl2.add(picLabel);
imagePnl2.repaint();
imagePnl2.revalidate();
}
else
{
System.out.println("File access cancelled by user.");
}
}
In this question the problem was the missing revalidate()
. But that makes no difference here.
What am I missing?