JavaNoob
JavaNoob

Reputation: 47

Error when trying to update a picture in JPanel

Please excuse my complex GUI structure, I am very new to this: URL of picture->ImageIcon->JLabel->JScrollPane->JPanel->JTabbedPane->JFrame.

The idea is to update the picture by having a button whose action updates part of the url. To ensure this button did change the url, I have a JTextfield which displays the url on button click. The url in text field shows the update did take place but the picture in GUI remains the same.

The class for creating image pane:

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class MapPicturePanel {
    public JScrollPane getContent(BufferedImage image) {
        ImageIcon icon = new ImageIcon(image);
        JLabel label = new JLabel(icon);
        label.setHorizontalAlignment(JLabel.CENTER);
        return new JScrollPane(label);        
    }



}

Thank you.

Upvotes: 0

Views: 108

Answers (2)

camickr
camickr

Reputation: 324207

Don't create a new panel every time you change the image.

Instead just read the image, create a new ImageIcon and then you can use:

label.setIcon(...);

and the label will automatically repaint itself.

Upvotes: 2

A.B.Cade
A.B.Cade

Reputation: 16915

Add the repaint() command after changing the URL

Upvotes: 1

Related Questions