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

[422 byte] By [linuxhippya] at [2007-11-27 4:28:28]
# 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

gracklemanna at 2007-7-12 9:37:14 > top of Java-index,Desktop,Core GUI APIs...
# 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.

camickra at 2007-7-12 9:37:14 > top of Java-index,Desktop,Core GUI APIs...
# 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

linuxhippya at 2007-7-12 9:37:14 > top of Java-index,Desktop,Core GUI APIs...