How do I wrap propertyChanged code into an event dispatch thread?
/*
* 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
*/
publicclass EventRunnableHandlerimplements 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 */
publicvoid run(){}
}
/**
* Invoked when task's progress property changes.
*/
publicvoid propertyChange(PropertyChangeEvent evt){
SwingUtilities.invokeLater(new EventRunnableHandler(evt){
publicvoid run(){
if (evt.getPropertyName() =="progress"){
int progress = (Integer)evt.getNewValue();
progressBar.setValue(progress);
}
}
});
}
This code fails to compile producing the error message:
local variable evt is accessed from within innerclass; needs to be declaredfinal
Bluntly put, how do I fix this?
Thanx
Phil
Double-posted http://forum.java.sun.com/thread.jspa?threadID=5148307@OP: You do exactly what it says. You declare your evt variable to be final.
I have no idea how to do that, please help.Phil
http://java.sun.com/docs/books/tutorial/java/IandI/final.html
I know what "final" is, but I don't know how to implement it within EventRunnableHandler, please show me.
final private EventObject evt;By the way if your propertyChange method is supposed to be in your EventRunnableHandler class, then you need to check out your brackets.
> I know what "final" is, but I don't know how to
> implement it within EventRunnableHandler, please show
> me.
I guessed it, never mind:
/**
* Invoked when task's progress property changes.
*/
public void propertyChange(final PropertyChangeEvent evt) {
SwingUtilities.invokeLater(new EventRunnableHandler(evt) {
public void run() {
if (evt.getPropertyName() == "progress") {
int progress = (Integer)evt.getNewValue();
progressBar.setValue(progress);
}
}
});
}
> final private EventObject evt;
>
> By the way if your propertyChange method is supposed
> to be in your EventRunnableHandler class, then you
> need to check out your brackets.
Why? EventRunnableHandler class is a generic class that handles Runnable methodology with the inputted parameter of EventObject object, whereas propertyChanged is specific to any class implmenting PropertyChangeListener. I don't understand your reasoning.
> Why? EventRunnableHandler class is a generic class that handles Runnable methodology
Not really. Since the run method is empty, you have to override it locally everytime you create an instance of it. It now just serves as a holder for the event, which isn't needed if you do this:
import java.beans.*;
import javax.swing.*;
public class Test implements PropertyChangeListener
{
JProgressBar progressBar;
public void propertyChange(final PropertyChangeEvent evt)
{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
if (evt.getPropertyName().equals("progress"))
{
int progress = (Integer)evt.getNewValue();
progressBar.setValue(progress);
}
}
});
}
}
By the way, don't use "==" when comparing Strings, use .equals("myString") instead.
> > Why? EventRunnableHandler class is a generic class
> that handles Runnable methodology
>
> Not really. Since the run method is empty, you have
> to override it locally everytime you create an
> instance of it. It now just serves as a holder for
> the event, which isn't needed if you do this:
> import java.beans.*;
> import javax.swing.*;
>
> public class Test implements PropertyChangeListener
> {
>JProgressBar progressBar;
>
> public void propertyChange(final
> PropertyChangeEvent evt)
> {
>SwingUtilities.invokeLater(new Runnable(){
>public void run(){
>if (evt.getPropertyName().equals("progress"))
>{
>int progress = (Integer)evt.getNewValue();
>progressBar.setValue(progress);
>}
> }
>});
>
> }
> By the way, don't use "==" when comparing
> Strings, use .equals("myString") instead.
I guess what I don't understand is that I am having to put "final" into the parameter declaration for propertyChanged(), which I was unaware that that is legal Java syntax, furthermore, why must it be declared final in the first place?
Any variable, local or a formal parameter, can be accessed by methods within an inner class so long as they are declared as final.
Right, I guess what I didn't know is that you can declare a parameter as final, I've never seen that syntax ever written before, and the only reason I realized it worked was because I literally guessed that it would without knowing that it would.
I'd still like to know why on earth you can't declare static properties or methods within inner classes, again, not knowing why on that one either, but that's not related to this.. thread.