I am uploading images dynamically via file chooser but the image doesn't fit the size of the JLabel. How to resolve it?
public void image()
{
fileChooser.setCurrentDirectory(new File("."));
fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
@Override
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".jpg")|| f.isDirectory();
}
@Override
public String getDescription() {
return "JPG Images";
}
});
int r = fileChooser.showOpenDialog(new JFrame());
ImageIcon ico=new ImageIcon(fileChooser.getSelectedFile().getAbsolutePath());
UpImage.setIcon(ico);
file = fileChooser.getSelectedFile();
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException ex) {
Logger.getLogger(EntryEmp.class.getName()).log(Level.SEVERE, null, ex);
}
}
Grab hold of the image with your
JFileChooserlike below:Now calculate the width and height of your JLabel, and send it to the method
resizeImage(BufferedImage image, int width, int height). TheresizeImagemethod is coded below.That's it. Now all you have to do is:
Hope this helps :)