Hi!
I'm trying to use the SwingWorker class in a ListCellRenderer
with no luck. My ListCellRenderer loads a scaled down JPEG file,
this means my application locks while loading a JList of
many items. So, I thought on using that SwingWorker class
to open a separate thread for this I/O intensive task. My problem
is that I don't know how to make it work in my ListCellRenderer.
The compiler always complains with either one of the following
messages:
"Cannot refer to a non-final variable 'x' inside a inner class
defined in a different method"
"The final local variable 'x' cannot be assigned, since it is defined
in an enclosing type"
I've read all the docs about the SwingWorker class in the Sun site
but I cannot get it to work.
Here is the code:
...
public Component getListCellRendererComponent(
JList list,
Object value, // value to display
int index, // cell index
boolean isSelected, // is the cell selected
boolean cellHasFocus) // the list and the cell have the focus
{
final File file = (File)value;
setText(file.getName());
if (file.isDirectory()) {
setImage(iconToBufferedImage(fileSystemView.getSystemIcon(file)));
} else {
BufferedImage thumbnail = (BufferedImage)thumbnails.get(value);
if (thumbnail == null) {
final SwingWorker worker = new SwingWorker() {
public Object construct() {
thumbnail=createImage(file);
return thumbnail;
}
public void finished() {
}
};
worker.start();
}
thumbnails.put(value,thumbnail);
setImage(thumbnail);
}
}
...
Can someone help me, please?
Thanks in advance

Signature
Abs
Tony Morris - 28 Mar 2004 23:51 GMT
> "Cannot refer to a non-final variable 'x' inside a inner class
> defined in a different method"
[quoted text clipped - 4 lines]
> I've read all the docs about the SwingWorker class in the Sun site
> but I cannot get it to work.
Yes, but did you bother to read your compiler error messages ?
Side note:
When you think something doesn't work, when that something has been used
successfully by many people, determine whether or not your assessment makes
logical sense (since, in this case, it doesn't).
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Jon A. Cruz - 29 Mar 2004 16:44 GMT
> "Cannot refer to a non-final variable 'x' inside a inner class
> defined in a different method"
>
> "The final local variable 'x' cannot be assigned, since it is defined
> in an enclosing type"
> Can someone help me, please?
Kinda hard to, since I see no variable 'x' in your code you supplied.
Andrew Thompson - 29 Mar 2004 17:40 GMT
>> "Cannot refer to a non-final variable 'x' inside a inner class
>> defined in a different method"
>>
>> "The final local variable 'x' cannot be assigned, since it is defined
>> in an enclosing type"
...
> ..I see no variable 'x' in your code you supplied.
<http://www.physci.org/codes/sscce.jsp>

Signature
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
Abs - 29 Mar 2004 18:15 GMT
>> "Cannot refer to a non-final variable 'x' inside a inner class
>> defined in a different method"
[quoted text clipped - 5 lines]
>
> Kinda hard to, since I see no variable 'x' in your code you supplied.
Sorry, the 'x' variable refers to the 'thumbnail' variable in the code.
Regards

Signature
Abs
Jon A. Cruz - 30 Mar 2004 05:32 GMT
> Sorry, the 'x' variable refers to the 'thumbnail' variable in the code.
That's why it's important to copy error messages completely, not just
type them in partially.
The problem is exactly as the error message is telling you.
However... you have a problem with the understanding. In general, you
have three parts to your code
1) You declare the BufferedImage
2) You have a SwingWorker to load it
3) You have presumably a Hashtable or some such where you try do put it.
However... block 2 will be executed at some later time, and will not run
until some time after part 3 has completed.
Try moving your last couple of statements there into the finished()
call, since you probably want to actually get the images before you
start to use them.
Abs - 30 Mar 2004 07:44 GMT
>> Sorry, the 'x' variable refers to the 'thumbnail' variable in the code.
>
[quoted text clipped - 16 lines]
> call, since you probably want to actually get the images before you
> start to use them.
Thanks, I can see the problem now, I'll try your solution.
--
Abs