I am writing an Eclipse Plugin that opens a file and displays all the images contained within the file. This image display is part of a GUI application. Each image is rendered by associating it with an SWT Canvas widget. When I open the file I have all the information I need to determine the number of images I will have to display. I though it would make sense to create all the Canvas objects, one after another and store each Canvas object in some type of array like data structure. Every file I open with have a different number of images to display. I decided to use an ArrayList.
I proceed as follows: I create a Canvas object for each image and store all of the Canvas objects in a ArrayList. Here's the problem: Each Canvas object has a PaintListener and MouseListener associated with it - for resizing and detecting if an image has been 'clicked.' I am creating all the Canvas objects in a 'for loop' - which includes assigning a PaintListener and MouseListener to each Canvas object, like so:
`
//
// Assume the following ArrayLists have been defined:
// myCanvases, myImages, and myFrames
//
for (int i = 0; i < numberOfImages; i++) {
canvas = new Canvas(getMyComposite(), SWT.BORDER |
SWT.NO_MERGE_PAINTS | SWT.NONE );
canvas.setLayoutData(getMyGridData());
.
.
.
canvas.addPaintListener(new PaintListener() {
public void paintControl(final PaintEvent event) {
if (myImages.get(i) != null) {
myImages.get(i).dispose();
event.gc.drawImage(mySceneImages.get(element), 0, 0);
}
}
});
currentCanvas.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent event) {
getVideo().setCurrentFrame(myFrames.get(i).getFrameNumber());
}
});
canvas.setVisible(true);
myCanvases.add(i, canvas);
} // End for (int i = 0; i < numberOfScenes; i++)
`
The Problem: The variable 'i' is used to determine which element to access in the different ArrayLists in the PaintListener and MouseListener. Note I am using 'i' for illustrative purposes here. I realize the variable 'i' defined in the 'for loop' cannot be used in the inner classes of the Listeners. Anyway...... When the Listeners receive an event, whatever variable I do use to attempt to access a specific element of an ArrayList contains it's present value, not the value when the Listeners were defined.
How can I get around this in Java? What I literally need is code in the definition of each Listener that essentially says equates to:
`
myCanvases.get(7); // or whatever the for loop iteration is for that specific object);
// Not
myCanvases.get(i); // - which will contain the present value of i;
`
when the Listener executes.
Any ideas would be appreciated.
Thanks.