1

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.

kinkajou
  • 3,664
  • 25
  • 75
  • 128

3 Answers3

1
.
.
File file = fileChooser.getSelectedFile();

JLabel label = new JLabel();
ImageIcon icon = new ImageIcon(file.getAbsolutePath());
label.setIcon(icon);
//add label to panel
Sully
  • 14,672
  • 5
  • 54
  • 79
  • 2
    What is with the `setLocation` and `setSize` calls ? A label is perfectly capable of calculating its own size, and the layout manager will determine the location – Robin Apr 25 '12 at 15:17
1
fileChooser.showDialog(saveBtn2, null);
File file = fileChooser.getSelectedFile();
System.out.println("The path to file "+file.getAbsolutePath());          
ImageIcon icon = new ImageIcon(file.getAbsolutePath());
pictureLbl.setIcon(icon);
eabraham
  • 4,094
  • 1
  • 23
  • 29
1

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • @ Kitex no idea, really you have to edit your question with SSCCE and use images from (for example) from (@Andrew Thompson) because nobody can see rest of your code and (don't want if I have got my) access to your image saved on your HDD :-) – mKorbel Apr 26 '12 at 23:35