On Mar 15, 3:15 pm, "Steve W. Jackson" <stevewjack...@knology.net>
In article <1173901512.766685.138...@b75g2000hsg.googlegroups.com>,
"phillip.s.pow...@gmail.com" <phillip.s.pow...@gmail.com> wrote:
[code]
/*
* EventRunnableHandler.java
*
* Created on March 14, 2007, 3:36 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.ppowell.tools.ObjectTools.SwingTools;
import java.util.EventObject;
/**
* Wrapper for Runnable interface that will allow for {@link
java.util.EventObject}
* @author Phil Powell
* @version JDK 1.6.0
*/
public class EventRunnableHandler implements Runnable {
/**
* {@link java.util.EventObject}
*/
private EventObject evt;
/**
* Creates a new instance of EventRunnableHandler
* @param evt {@link java.util.EventObject}
*/
public EventRunnableHandler(EventObject evt) {
this.evt = evt;
}
/** Perform run */
public void run() {}
}
/**
* Invoked when task's progress property changes.
*/
public void propertyChange(PropertyChangeEvent evt) {
SwingUtilities.invokeLater(new EventRunnableHandler(evt) {
public void run() {
if (evt.getPropertyName() == "progress") {
int progress = (Integer)evt.getNewValue();
progressBar.setValue(progress);
}
}
});
}
[/code]
This code fails to compile producing the error message:
[code]
local variable evt is accessed from within inner class; needs to be
declared final
[/code]
Bluntly put, how do I fix this?
Thanx
Phil
I'm not sure if handling a PropertyChangeEvent on the EDT is really
wise, but I'll set that aside...
There are two techniques I occasionally use for the specific problem
you're trying to solve. One is to make the parameter of the method
final, so that your "public void propertyChange" would declare its "evt"
parameter as final. Of course, it isn't always possible to use that
approach, so the second is to create another variable.
The complaint is telling you that the variable being handled must either
be a member variable in your EventRunnableHandler class, or that it must
be final. So I frequently do something like this:
final PropertyChangeEvent fpce = evt; (in the context of your code)
Then the subsequent code would use "fpce" instead of "evt".
HTH.