If you look at the ImageIcon constructor, you will see that it loads the icon using this method: image = Toolkit.getDefaultToolkit().getImage(location); The documentation for this method says:
* Returns an image which gets pixel data from the specified URL. * The pixel data referenced by the specified URL must be in one * of the following formats: GIF, JPEG or PNG. * The underlying toolkit attempts to resolve multiple requests * with the same URL to the same returned Image.
To update ImageIcon every time you load it from the same file name, you must add something random to the end of the URL without changing the actual file name. Something like this should work:
try { URL url = new URL(file.toURI().toString() + "?" + System.currentTimeMillis()); jLabel1.setIcon(new ImageIcon(url)); } catch (MalformedURLException ex) { ex.printStackTrace(); }
source share