Skip to main content

Accessing variable from EDT

1 reply [Last post]
Joined: 2009-03-25

I'm writing a program to convert data coming from an external supplier of ours into our format which is mostly an automatic process but sometimes the data needs to be editted and so I display a JDialog to do that.

Once I've displayed the dialog, depending on which button was clicked to close it (either continue of skip) I add the object passed to the dialog for editting into an array (used for processing later) and this is where the problem is. Because I've decided to use the substance l&f the dialog needs to be created on the EDT but the array exists outside of it and without declaring the array as final (which is pointless) I can't get the code to compile:

List products = getProductsToUpdate(f.getReleases().getRelease());</p>
<p>if (f.getReleases().getRelease().size() > 0 && products.size() == 0) {<br />
    java.awt.EventQueue.invokeAndWait(new Runnable() {<br />
        public void run() {<br />
            VSProductDialog d = new VSProductDialog(transformProduct(f), f);<br />
<p>            if (d.getResult() == VSProductDialog.CONTINUE) {<br />
                products.add(d.getProduct());<br />
            }<br />
        }<br />
    });<br />

I'm fairly new to java and I have absolutely no idea how to even start getting around this. Can anyone help?


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2007-04-17

Declaring the list as final is the correct solution, not pointless. If transform(f) can take a while as well, better do it off the EDT as well (with a final Product transformed = transformProduct(f)).

Note that final only makes the reference to the list final, not the list contents. And since you use invokeAndWait, you shouldn't get any threading problems.