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

[2688 byte] By [ppowell777a] at [2007-11-26 21:38:29]
# 1
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.
Djaunla at 2007-7-10 3:21:31 > top of Java-index,Java Essentials,New To Java...
# 2
I have no idea how to do that, please help.Phil
ppowell777a at 2007-7-10 3:21:31 > top of Java-index,Java Essentials,New To Java...
# 3
http://java.sun.com/docs/books/tutorial/java/IandI/final.html
Djaunla at 2007-7-10 3:21:31 > top of Java-index,Java Essentials,New To Java...
# 4
I know what "final" is, but I don't know how to implement it within EventRunnableHandler, please show me.
ppowell777a at 2007-7-10 3:21:31 > top of Java-index,Java Essentials,New To Java...
# 5
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.
Djaunla at 2007-7-10 3:21:31 > top of Java-index,Java Essentials,New To Java...
# 6

> 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);

}

}

});

}

ppowell777a at 2007-7-10 3:21:31 > top of Java-index,Java Essentials,New To Java...
# 7

> 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.

ppowell777a at 2007-7-10 3:21:31 > top of Java-index,Java Essentials,New To Java...
# 8

> 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.

KelVarnsona at 2007-7-10 3:21:31 > top of Java-index,Java Essentials,New To Java...
# 9

> > 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?

ppowell777a at 2007-7-10 3:21:31 > top of Java-index,Java Essentials,New To Java...
# 10
Any variable, local or a formal parameter, can be accessed by methods within an inner class so long as they are declared as final.
filestreama at 2007-7-10 3:21:31 > top of Java-index,Java Essentials,New To Java...
# 11

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.

ppowell777a at 2007-7-10 3:21:31 > top of Java-index,Java Essentials,New To Java...