user3314570
user3314570

Reputation: 237

Can't paint an image after choosing it from JFileChooser

Good evening. I have read a lot of topics here on stackoverflow or even internet but I can't find the solution to my problem.

I have an interface like this: enter image description here

When I click on "Load Image A", I can choose the image that I want. Next I want to paint this image under the JLabel "Image A". But it doesn't want to show up.

Here is the code I wrote:

package projet;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class MonPanelImage extends JPanel{

    private static final long serialVersionUID = -8267224342030244581L;
    private BufferedImage image; 

    public MonPanelImage(File adresse)
    {
        try{
            image = ImageIO.read(adresse);
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponents(g);
        System.out.println("paint");
        if(image != null){
             g.drawImage(image, 20, 20, this);
        }
    }
}

and here is where I call it:

    //panel image. This is my second panel which will be for the images
    final JPanel second = new JPanel(new BorderLayout());

    //panel button. This is the third panel for the buttons
    rows = 0;
    cols = 3;
    hgap = 5;
    vgap = 0;
    JPanel third = new JPanel(new GridLayout(rows,cols,hgap,vgap));     
    //buttons
    JButton boutonLoad1 = new JButton("Load image A");
    boutonLoad1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int retour = fc.showDialog(frame, "Charger l'image");
            if(retour == JFileChooser.APPROVE_OPTION){              
                String pathImage1 = fc.getSelectedFile().getAbsolutePath();
                path1 = pathImage1; 

                File file = fc.getSelectedFile();
                MonPanelImage panelImage1 = new MonPanelImage(file);
                second.add(panelImage1, BorderLayout.WEST);
                second.revalidate();
                second.repaint();
            }
        }
    });

At the very end, i add the 3 panels to my frame and set the frame to visible. But I can't paint an image. Maybe I'm not doing it properly. Can someone help me please?

Thanks

Upvotes: 0

Views: 142

Answers (1)

camickr
camickr

Reputation: 324128

super.paintComponents(g);

First of all it should be super.paintComponent(g), without the "s".

second.add(panelImage1, BorderLayout.WEST);

You are adding your image to a component using a BorderLayout. The BorderLayout will respect the width of your component, which is 0, so there is nothing to paint.

Whenever, you do custom painting you need to override the getPreferredSize() method to return the size of your component so the layout manager can do its job.

However, an easier solution is to just use a JLabel with an Icon. There is no need to do custom painting when you are painting the image at its real size.

Upvotes: 2

Related Questions