I have this class. Upon initialization, I see the image I want, but when I call load() a second or third time, the image doesn't refresh. How can I repaint the image?
public class ImgPanel extends JPanel { JLabel imgLbl; public ImgPanel(String initPath) { load( initPath); add( imgLbl ); } public void load(String path) { BufferedImage img = ImageIO.read( new File( path ) ); imgLbl = new JLabel(new ImageIcon( img ) ); // repaint(); <-- no effect // imgLbl.repaint(); <-- no effect } }
I'm also trying this way (same issue)
public class ImgPanel extends JPanel{ BufferedImage img; public ImgPanel(String initPath) { load( initPath ); } public void load(String path) { img = ImageIO.read(new File(path) ); repaint(); } protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(img, 0, 0, null); } }