'JLabel not displaying image after choosing from jFileChooser in java netbeans IDE 8.02
I am using NetBeans IDE 8.0.2 to create my project application. I have created my application's signup page using multiple panels in CardLayout. For users to upload their image I created a JLabel and added mouseClicked event to it which displays JFileChooser but the image chosen is not displayed in the label (even though the image path is stored). Please check the following code.
private void PrImgLabMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
JFileChooser FileChooser = new JFileChooser();
int response = FileChooser.showOpenDialog(Panel1);
if(response == JFileChooser.APPROVE_OPTION){
File imgFile = FileChooser.getSelectedFile();
String Filepath = imgFile.getAbsolutePath();
ImgMeth.Resize(Filepath, PrImgLab);
}
else if(response == JFileChooser.CANCEL_OPTION);{
Icon defimg = new ImageIcon("DefImg.png"); //default image path
PrImgLab.setIcon(defimg);
}
}
This the label event class and the code below is the method used to resize the image and fit it into the JLabel.
public void Resize(String path,JLabel label){
ImageIcon img = new ImageIcon(path);
Image image = img.getImage();
Image imgScale = image.getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_SMOOTH);
Icon icon = new ImageIcon(imgScale);
label.setIcon(icon);
}
Solution 1:[1]
After calling resize (Yes that should begin lower case) call validate on the Container containing the label.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | g00se |
