Is it allowed to do loading of imageicons outside of the EDT?
Hello,
Is it allowed to load ImageIcons outside of the EDT? I would like to do this for Java-1.3.1 where advanced tools like SwingWorker don't exist and I am a bit afraid because its always said swing isn't thread safe.
So may this code be run outside of the EDT:
Image nativeImage = new ImageIcon(new URL("http://localhost:5555/data/" + name)).getImage();
Thank you in advance, lg Clemens
# 1
Loading the image on a separe thread should be fine. The question is updating the GUI once it is loaded.
The way I understand it (which may be incorrect) is that you would not want the image added to the GUI while it is loading on a separate thread. Once it is loaded though, you can then add it to the GUI by using
SwingUtilities.invokeLater(..).
which will execute code on the EDT
# 2
> I am a bit afraid because its always said swing isn't thread safe.
Thats referring to updating the state of a GUI component. An ImageIcon isn't a GUI component.
> Is it allowed to load ImageIcons outside of the EDT?
In fact you would want to do this outside of the EDT. Loading an image from a URL will take time and will block the EDT, so you should be using a separate Thread.
Once the icon is loaded and you want to display the icon in a label, for example, then you would use the SwingUtilities.invokeLater(...) method to invoke the label.setIcon(...) method to make sure it executes on the EDT as mentioned above.
# 3
Thanks for the tips and suggestions.
Yes I always update my Widgets in the EDT, but I also was a bit afraid since ImageIcon uses AWT-Classes internally ... and who knows wther its safe to call them outside the EDT.
Glad that this is allowed, makes things much easier for me :-)
Thanks, lg Clemens