Search code examples
javaswingjlabel

Rendering Image into label


I have a label. I want to render image into it. But the following code would do anything.

 CardLayout cl = (CardLayout) (mainPanel.getLayout());
        cl.show(mainPanel, "newPersonaCard");
        BufferedImage myPicture = ImageIO.read(new    File("C:\\Desktop\\Documents\\Pictures\\always.jpg"));
        ImageIcon icon = new ImageIcon(myPicture);
        icon.getImage().flush();

I am using netbean designer.


Solution

  • You are right, in some cases there issue with repainting Icon in the JLabel, then you have to call,

    myIcon.getImage().flush();
    myLabel.setIcon(myIcon);
    

    rest of methods is implemented in the Icon and JLabel correctly