Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Updating a JLabel's icon - Urgent!

807607Jan 10 2007 — edited Feb 2 2007
Hi all,
Any help you can give would be greatly appreciated...

I have a JLabel on a JPanel. The JLabel is used to display a picture. It works fine once. However, after the first picture is drawn, I cannot get it to update the picture with subsequant calls.

In my constructor I have:
JLabel theLabel; // The label to display the image
public Constructor() {
     /* Add blank image to panel */
     visualPanel = new JPanel(); // Create panel
     ImageIcon pic = new ImageIcon(); // Create image icon
     theLabel = new JLabel(pic, JLabel.CENTER);   // Create label
     p.setOpaque(true); // Set opaque
     visualPanel.add(theLabel); // Add to panel
}
And I have a separate method for updating the picture displayed by theLabel:
public void showFrame(){
     ImageIcon pic = new ImageIcon("frame.jpg"); // Load picture
     theLabel.setIcon(pic);	// Set theLabel's icon
}
So to summarise: I call showFrame once, it displays the picture, I call it again, it doesn't get repainted. I have tried calling paint() on the visualPanel, on its parent panel (not listed above), and on theLabel, but with no luck.

How can I make theLabel repaint with the new ImageIcon? Thanks in advance!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 2 2007
Added on Jan 10 2007
35 comments
1,564 views