Fred Fred - 1 year ago 147
Java Question

JAVA - Add an image to JPanel

I'm new in JAVA.
I would like to get an image in my window (JPanel) for working on it (add circles for example).
I created a menu and when I click in "File>Import", the open dialog box is appearing to choose my image. I get the right path to the image file (checked thanks to System.out.println(FC.getSelectedFile().toString());) but the image doesn't appear...

Here is the code :

//On crée les listeners pour le menu "Fichier" :
this.importer.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent event)
//On ouvre la boîte de dialogue pour charger le dessin :
JFileChooser FC = new JFileChooser();
BufferedImage myPicture=null;
try {
myPicture =;
} catch (IOException e) {
// TODO Auto-generated catch block
contenant.add(new JLabel(new ImageIcon(myPicture)), BorderLayout.CENTER);

Thanks for your support.

Answer Source

Replace this:


With this:


When you add the JLabel, it doesn't automatically force the content pane to sort out it's layout, so even though the JLabel is on there, it's not sized yet. Calling revalidate() fixes this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download