Friday, June 01, 2007

Colocating images with Wicket

Say you want to keep all your images in one directory for easy management. It's not always possible with Wicket 1.2.x to keep your images in WEB-INF/images. Wicket wants them to be in the source hierarchy. For example, say you are using a wicket.markup.html.tree.Tree, and you want to override the node images. You need to provide a wicket.markup.html.image.Image, as follows:
public class NavTree extends Tree
{
@Override
protected Image getNodeImage(DefaultMutableTreeNode node)
{
...
}
}

To colocate my images for such purposes, I created a package, say com.my.images, and in that directory, I created an empty class called ImageAnchor.java. Then to create my image I can call:
new Image(Tree.NODE_IMAGE_NAME,
new ResourceReference(com.my.images.ImageAnchor.class, "penguin.gif"));

But what if I also want to refer to my images from html or css? Fortunately, there is a way to do this. For example, in CSS:
background-image:url(/app/resources/com.my.images.ImageAnchor/penguin.gif);

4 comments:

cricket all day .. said...
This comment has been removed by the author.
cricket all day .. said...
This comment has been removed by the author.
helloworld said...

so, you have all your imaegs in src/com/my/images? Or you create web-inf.images.ImageAnchor ?

Julian Sinai said...

Yes, all my images and ImageAnchor.java are in src/com/my/images.