简体   繁体   中英

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.

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.

更改URL后添加repaint()命令

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM