How to display an image on Java Swing

Images can be displayed in Java Swing using object JLabel.

This is the partial code:

JLabel label = new JLabel();
try {
BufferedImage img = ImageIO.read(new File(IMG_FILES_PATH + IMG_FILENAME));
ImageIcon icon = new ImageIcon(img);
label.setIcon(icon);
} catch (IOException ex) {
Logger.getLogger(SwingGUIMainScreen.class.getName()).log(Level.SEVERE, null, ex);
}

If I need the image to have an specific size, what I do is resizing the BufferedImage object. Read this other post to find more about it.

Leave a Reply

Your email address will not be published. Required fields are marked *