How to place an image in a Java Swing GUI


I'm new to Swing and just need to know how to place an image on the home screen of my GUI. I don't want it to be the background. When my lecturer demoed it I think he placed it inside a JLabel. I saved the image to my laptop, but do I need to import it into the project I'm working on in Eclipse? If so, where is the best place to save it to? Thanks.

Put all the images in images folder that you have in your project in parallel to src folder.


Use this code File("images\\c.jpg")); File("images\\d.jpg"));

You can try any one

// Read from same package"c.png"));

// Read from absolute path File("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\c.png"));

// Read from images folder parallel to src in your project File("images\\c.jpg"));

All the above method returns BufferedImage.

How to convert BufferedImage into ImageIcon?

BufferedImage image = File("images\\c.jpg"));
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);