- Reload Icon Images
Suppose we have a JLabel with an icon:
JLabel testLabel = new JLabel();
ImageIcon img = new ImageIcon("test.png");
testLabel.setIcon(img);
and we want to update the icon image from the same file due to external changes.
The following code won't work:
ImageIcon img = new ImageIcon("test.png");
testLabel.setIcon(img);
testLabel.repaint();
The reason is that the image is cached. The JLabel was indeed
repainted, but the image was not reloaded from the disk. We need to flush the cached image
before repainting:
((ImageIcon)graph.getIcon()).getImage().flush();
ImageIcon img = new ImageIcon("test.png");
testLabel.setIcon(img);
testLabel.repaint();
|