I would like to add a click handler to an image that changes the size of the image. The BlobStore allows one to easily change the size of an image by changing the suffix of the URL, as “=s128” will change the image to 128 pixels in the line below:
im.setUrl(thing.get(i)+"=s128");
I want to use this feature to change the size of an image after it is clicked; in this example to change back to its original size, like:
//-- when clicked, do this:
im.setUrl(thing.get(i));
and I thought the following code would do it for a group of images:
for (int i=0; i<thing.size(); i++){
final Image im = new Image();
im.setUrl(thing.get(i)+"=s128");
im.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event){
im.setUrl(response.get(i));
}
});
htmlpanel.add(im,"imageGrid");
}
But the second “setUrl” line gives me this error:
Multiple markers at this line
– Cannot refer to a non-final variable response inside an inner class defined in a different method
– Cannot refer to a non-final variable i inside an inner class defined in a different method
How can I do this? Thanks.
responseandivariables should be final. That why compiler can’t compile this code. But you can’t makeifinal because you are incrementing it in inforcycle. So you’ll have to copy it into some final variable:But the best solution will be to create special click handler class, which will set a specific URL to specific Image.
UPDATE
This structure:
is called an anonymous class. If it needs to access some variable/parameter in method which defines it, such variable/parameter have to be final.