Note that there are some explanatory texts on larger screens.

plurals
  1. POEclipse SWT - Java Programming Dilemma
    primarykey
    data
    text
    <p>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 <code>ArrayList</code>. </p> <p>I proceed as follows: I create a Canvas object for each image and store all of the Canvas objects in a <code>ArrayList</code>. Here's the problem: Each Canvas object has a <code>PaintListener</code> and <code>MouseListener</code> 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 <code>PaintListener</code> and <code>MouseListener</code> to each Canvas object, like so:</p> <p>`</p> <pre><code>// // Assume the following ArrayLists have been defined: // myCanvases, myImages, and myFrames // for (int i = 0; i &lt; 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 &lt; numberOfScenes; i++) </code></pre> <p>`</p> <p><strong>The Problem</strong>: The variable 'i' is used to determine which element to access in the different ArrayLists in the <code>PaintListener</code> and <code>MouseListener</code>. 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 <strong>contains it's present value, not the value when the Listeners were defined.</strong></p> <p>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:</p> <p>`</p> <pre><code>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; </code></pre> <p>`</p> <p>when the Listener executes.</p> <p>Any ideas would be appreciated.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload