Adding images for ClassResourceObject

Hey,

I have recently installed the Vaadin plugin for Eclipse and I am currently reading through the book. You can say that I am complete newbie to Vaadin.

This might be a very stupid question :frowning: . I am trying to create a classResource object for a specific image. However, I do not know in which folder in my workspace/eclipse I am supposed to keep my image so that my ClassResource object can access it.

The book says that image has to be included in the JAR of the Web application ( in the default package). I don’t really understand what this means. In what folder should I put the image and how can I call that image using the ClassResource object so that I can use it on any labels I create and so on.

I know this must be a very stupid question to most of you. However, I have not played around with Vaadin for more than a week.

Any help would be greatly appreciated.

Thanks,
Rohith

There’s no such thing as a stupid question :slight_smile:

To access images through ClassResources, you should put your image in the CLASSPATH. So in eclipse it should be enough to put your image in the same package as your Application class:

and use it like this:

package com.example.helloworld;

import com.vaadin.Application;
import com.vaadin.terminal.ClassResource;
import com.vaadin.terminal.Resource;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Window;

public class HelloworldApplication extends Application {
	private static final long serialVersionUID = -6505074926320503537L;

	@Override
	public void init() {
		Window mainWindow = new Window("Hello");
		Resource myImageResource = new ClassResource("image.png", this);
		mainWindow.addComponent(new Embedded("My pretty image", myImageResource));
		setMainWindow(mainWindow);
	}
}

Also look into ThemeResource for images that you might want to change depending on which theme you set for your application.

HTH,
/Jonatan

Hey Jonatan,

Thank you so much. It was really helpful.

Rohith

That is very clear indeed. However consider this: I have an application that can be delivered either as a rich non-web client or as a web-based (vaadin) client.
The icons for my buttons are in a common
resources.jar
file that is on the classpath of the vaadin application/project:

I tested that going navigating directories worked when image.png is in another package in the Vaadin project. Here are my packages:

[com.example.helloworld]

     HelloWorld.java
[icons]
 -> in the Vaadin project
     image.png

With the adaptation of your example as follows :

Resource myImageResource = new ClassResource("../../../icons/image.png", this);

This works well when the icons directory is in the Vaadin project, but when it is packaged in a jar on the classpath, the icon does not show up

How could I achieve this ?

Thanks,

Damien

I figured it out myself. the problem lies in the difference between Class.getResourceAsStream and ClassLoader.getSystemResourceAsStream

I ended up in extending ClassResource as follows:

public class ClassPathResource extends ClassResource {

    private String resName;

	public ClassPathResource(String resourceName, Application application) {
    	super(resourceName, application);
    	this.resName = resourceName;
    }

	@Override
	public DownloadStream getStream() {
        final DownloadStream ds = new DownloadStream(
                [b]
ClassLoader.getSystemResourceAsStream(resName), getMIMEType()
[/b],
                getFilename());
        ds.setBufferSize(getBufferSize());
        ds.setCacheTime(getCacheTime());
        return ds;
	}
}

Does that make sense ?

I’m having the exact same issue as Damian, could someone shed some light here please?

Thanks,

Mark