JavaBeans and Images

Most bean building tools I've discovered so far have no way to stick images or icons on JLabels or JButtons, which is a bit of a pain. I assume this comes down to the fact that Image isn't serialisable, so as soon as you put one on a JLabel for instance, the JLabel isn't really a bean.

I was thinking of a few ways around it ..

(1) Make a sub-class of Image which is serialisable, and has two instance variables, (a) an image object, which we'll make transient, and (b) a resource identifier that can be used to get to this image, perhaps through JNDI. That way, it's serialisable because the only member is a string.

(2) Make a sub-class of Icon which does a similar thing.

(3) Make wrappers around JLabel, JButton, etc, and again, do the same thing.

Number 3 is the only one which works easily using the existing GUIs, but it strikes me as a rather heavy-handed way to get the functionality we want here. "All we want" is a way to drag and drop images and image-rendered components to make games. (Although, now that I look at the requirement, it sounds a bit like doctor evil asking for sharks with laser beams on their heads.)

Number 1 and 2 would be really clean, if only the building tools around had the concept of images. (Hell, JBuilder doesn't even allow you to select the icons for buttons using its visual designer. It has a drop down box for it, which is always empty. Why bother?)

Is there any obvious way around all this, or will we be wrapping every single useful swing class to get this functionality? Perhaps there's a bean builder out there that can do images with less pain?

[1658 byte] By [trejkaz] at [2007-9-26 9:08:24]
# 1

try this:

java.net.URL url = getClass().getResource("icons/hello.gif");

Image image = Toolkit.getDefaultToolkit().getImage(url);

ImageIcon imageIcon = new ImageIcon(image);

JButton button = new JButton(imageIcon);

oj

olivierjeudy at 2007-7-1 20:15:55 > top of Java-index,Desktop,Developing for the Desktop...
# 2

Yeah, I know how to create a button with an image on it. Everybody knows how to create a button with an image on it. :-)

There are two issues here.

The main stopper at this moment is that the new XMLEncoder doesn't know what to do about ImageIcon. Even if it did, putting the image data into an XML file probably wouldn't be the best way to save space. :-)

The other problem is that JBuilder (and many similar IDEs) expose the icon property but don't allow you to set an image to it. We were thinking making a String property available because it would let these bean builders make it easier for people to put in images... but really, this would only be a hack, at best.

trejkaz at 2007-7-1 20:15:55 > top of Java-index,Desktop,Developing for the Desktop...
# 3

I am not sure of what you want. If you mean that you'd like to attach an Icon to a JButton/JLabel using a tool such as the Beanbox, then I think that the String property solution you have in mind is good.

You put all the icons in the same "icon" folder. You create a String property which values are limited to the icon names contained in the "icon" folder (you can use java.io to list all the names of the .gif (or whatever) files in the folder, and create an array out of it). The user will then be able to select, from a tool like the Beanbox, the icon he wants to use.

This solution seems simple and efficient. You seem to think that it's not appropriate. Why?

oj

olivierjeudy at 2007-7-1 20:15:55 > top of Java-index,Desktop,Developing for the Desktop...
# 4

The only reason I felt that it wasn't appropriate was because it makes us sub-class a whole range of JComponent classes, just to provide as a work-around for a limitation in the editor. It means that it goes out the window as soon as someone makes a new kind of component that requires an Image as a property.

It does look like being the only way to do it though, at least until Borland and whoever else can't stick images on, clean up their act. :-(

trejkaz at 2007-7-1 20:15:55 > top of Java-index,Desktop,Developing for the Desktop...