Is it possible to get the location of the file that an ImageIcon was created from?
Internally ImageIcon has a transient private URL of its location, which when debu
You can use reflection to access the private field, but if the ImageIcon
wasn't created with a URL
the field will be null
. If you are creating the ImageIcon
s yourself you could keep track of the URL
s in a map.
ImageIcon icon = ...
Class extends ImageIcon> clazz = icon.getClass();
Field urlField = clazz.getDeclaredField("location");
urlField.setAccessible(true);
URL location = (URL) urlField.get(icon);
It is also worth considering that the field name may change in future versions, which may produce exceptions.