ImageIcon do not update for a new image with the same URL

java, swing

Solution

The image is cached. Flush to clear the cache:

imageIcon.getImage().flush();

Problem

I would like to use JLabel(Icon) to show a Image which is from my website(http://xxx.xxx.xxx.xxx/java_pic/test.jpg). And I have a refresh button to new a new JLabel and ImageIcon(in order to get the newest image) The program run successfully...but when I upload a new image to override the old one(http://xxx.xxx.xxx.xxx/java_pic/test.jpg), I press the refresh button... nothing happened! I restart my program... and the new image now appears... why? Shouldn't it reload the image from website when I new a ImageIcon again? ``` public void refresh(){ URL iconUri = null; iconUri = new URL("http://XXX.XXX.XXX.XXX/java_pic/test.jpg"); ImageIcon imageIcon = new ImageIcon(iconUri); JLabel imageLabel = new JLabel(imageIcon); frame.add(imageLabel); ... ... } ``` when I click the refresh button, it would call the refresh()...why? Thanks!

Original source