SwingWorker's @Override process() error ?
Folks,
I'm stumped... I'm trying to get "interim results from the worker thread" (Flipper.java) working from the [url=http://java.sun.com/docs/books/tutorial/uiswing/concurrency/interim.html]Swing Tutorial[/url]/
I get this compiler error:Flipper.java:89: method does not override or implement a method from a supertype
... This is line 89 & 90 of Flipper.java ....
@Override
protectedvoid process(List<FlipPair> pairs){
but the [url=http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html]SwingWorker javadoc[/url] says:protectedvoid process(List<V> chunks);
// Receives data chunks from the publish method asynchronously on the Event Dispatch Thread.
The whole application isFlipper.java:import java.util.List;
import java.util.Random;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.BorderFactory;
import javax.swing.SwingWorker;
publicclass Flipperextends JFrameimplements ActionListener{
publicstaticfinallong serialVersionUID = 79843532;
privatefinal GridBagConstraints constraints;
privatefinal JTextField headsText, totalText, devText;
privatefinal Border border = BorderFactory.createLoweredBevelBorder();
privatefinal JButton startButton, stopButton;
private FlipTask flipTask;
private JTextField makeText(){
JTextField t =new JTextField(20);
t.setEditable(false);
t.setHorizontalAlignment(JTextField.RIGHT);
t.setBorder(border);
getContentPane().add(t, constraints);
return t;
}
private JButton makeButton(String caption){
JButton b =new JButton(caption);
b.setActionCommand(caption);
b.addActionListener(this);
getContentPane().add(b, constraints);
return b;
}
public Flipper(){
super("Flipper");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Make text boxes
getContentPane().setLayout(new GridBagLayout());
constraints =new GridBagConstraints();
constraints.insets =new Insets(3, 10, 3, 10);
headsText = makeText();
totalText = makeText();
devText = makeText();
//Make buttons
startButton = makeButton("Start");
stopButton = makeButton("Stop");
stopButton.setEnabled(false);
//Display the window.
pack();
setVisible(true);
}
privatestaticclass FlipPair{
privatefinallong heads, total;
FlipPair(long heads,long total){
this.heads = heads;
this.total = total;
}
}
privateclass FlipTaskextends SwingWorker<Void, FlipPair>{
@Override
protected Void doInBackground(){
System.out.println("DEBUG: doInBackground");
long heads = 0;
long total = 0;
Random random =new Random();
while (!isCancelled()){
try{ Thread.sleep(random.nextInt(50));}catch (InterruptedException ignore){}
total++;
if (random.nextBoolean()){
heads++;
}
publish(new FlipPair(heads, total));
//System.out.println("DEBUG: FlipTask main loop");
}
returnnull;
}
@Override
protectedvoid process(List<FlipPair> pairs){
System.out.println("DEBUG: process");
FlipPair pair = pairs.get(pairs.size() - 1);
headsText.setText(String.format("%d", pair.heads));
totalText.setText(String.format("%d", pair.total));
double bias = ((double) pair.heads) / ((double) pair.total) - 0.5;
devText.setText(String.format("%.10g", bias));
}
}
publicvoid actionPerformed(ActionEvent e){
if ("Start".equals(e.getActionCommand())){
startButton.setEnabled(false);
stopButton.setEnabled(true);
(flipTask =new FlipTask()).execute();
}elseif ("Stop".equals(e.getActionCommand())){
startButton.setEnabled(true);
stopButton.setEnabled(false);
flipTask.cancel(true);
flipTask =null;
}
}
publicstaticvoid main(String[] args){
SwingUtilities.invokeLater(
new Runnable(){
publicvoid run(){
new Flipper();
}
}
);
}
}
I tried just commenting out the @Override... and it compiles OK but I don't see the "DEBUG: process" message... I've tried it with and without the Thread.sleep (?which I suspect is "bad" in a Swing app?)
It looks like the main GUI event thread is blocked, but by what?
Please, any assistance would be appreciated.
Keith.
corlettk: oooops I just realised this question belongs on the SWING FORUM, so I'm going to cross post it there... Sorry. Flame me!
Oh, and I'm using java version "1.6.0-beta2" on an AMD dual core 64 running XP.
[9726 byte] By [
corlettka] at [2007-11-26 17:14:50]

This compiles and runs as expected in 1.6 .
import java.util.*;
import javax.swing.*;
public class Fred901
{
private static class FlipPair
{
private final long heads, total;
FlipPair(long heads, long total)
{
this.heads = heads;
this.total = total;
}
public String toString()
{
return heads + "\t" + total;
}
}
private class FlipTask extends SwingWorker<Void, FlipPair>
{
@Override
protected Void doInBackground()
{
System.out.println("DEBUG: doInBackground");
publish(new FlipPair(100,200));
publish(new FlipPair(101,201));
publish(new FlipPair(102,202));
publish(new FlipPair(103,203));
return null;
}
@Override
protected void process(List<FlipPair> pairs)
{
System.out.println("DEBUG: process");
for (FlipPair pair : pairs)
{
System.out.println(pair);
}
}
@Override
protected void done()
{
System.out.println("DEBUG: done");
}
}
public static void main(String[] args) throws Exception
{
FlipTask task = new Fred901().new FlipTask();
task.execute();
}
}
thanx saber...
I just tried to compile Fred901.java with the command"C:\Program Files\Java\jdk1.6.0\bin\javac" -Xlint -d c:\java\home\classes -classpath c:\java\home\src;. Fred901.java
but I get the same "method does not override" error- compile -
Fred901.java:36: method does not override or implement a method from a supertype
@Override
^
1 error
Output completed (2 sec consumed) - Normal Termination
, so I reckon it's definately an "environmental" problem ... and I'm almost certain that the compiler is finding an old (pre 1.6) version of SwingWorker.class.
But how to find out which one the class loader is finding ... looks like I've got some light reading ahead of me, which I can't face tonight. There is allways tomorrow.
Keith.
Message was edited by: corlettk
Yep it's environmental... emphasis on the mental.
Hey Sun guys !!! (one presumes they tail the forums) what's going on here?
C:\Java\home\src\forums>"C:\Program Files\Java\jdk1.6.0\bin\javac" -verbose -Xlint -d c:\java\home\classes -classpath c:\java\home\s
rc;. Fred901.java
[search path for source files: [c:\java\home\src, .]]
[search path for class files: [C:\Program Files\Java\jdk1.6.0\jre\lib\resources.jar, C:\Program Files\Java\jdk1.6.0\jre\lib\rt.jar,
C:\Program Files\Java\jdk1.6.0\jre\lib\sunrsasign.jar, C:\Program Files\Java\jdk1.6.0\jre\lib\jsse.jar, C:\Program Files\Java\jdk1.6
.0\jre\lib\jce.jar, C:\Program Files\Java\jdk1.6.0\jre\lib\charsets.jar, C:\Program Files\Java\jdk1.6.0\jre\classes, C:\Program File
s\Java\jdk1.6.0\jre\lib\ext\dnsns.jar, C:\Program Files\Java\jdk1.6.0\jre\lib\ext\localedata.jar, C:\Program Files\Java\jdk1.6.0\jre
\lib\ext\mail-1.4.1ea-SNAPSHOT.jar, C:\Program Files\Java\jdk1.6.0\jre\lib\ext\sunjce_provider.jar, C:\Program Files\Java\jdk1.6.0\j
re\lib\ext\sunmscapi.jar, C:\Program Files\Java\jdk1.6.0\jre\lib\ext\sunpkcs11.jar, c:\java\home\src, .]]
[parsing started Fred901.java]
[parsing completed 16ms]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Object.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/String.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Exception.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/javax/swing/SwingWorker.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Void.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/util/concurrent/RunnableFuture.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Runnable.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/util/concurrent/Future.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/util/List.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Override.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/annotation/Annotation.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/annotation/Target.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/annotation/ElementType.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/annotation/Retention.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/annotation/RetentionPolicy.class)]
[checking Fred901]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/Serializable.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/InterruptedException.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/util/concurrent/ExecutionException.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/util/concurrent/TimeoutException.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Error.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Throwable.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/RuntimeException.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/System.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/PrintStream.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/FilterOutputStream.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/io/OutputStream.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Iterable.class)]
[loading C:\Program Files\Java\jdk1.6.0\lib\ct.sym(META-INF/sym/rt.jar/java/util/Collection.class)]
Fred901.java:36: method does not override or implement a method from a supertype
@Override
^
[total 375ms]
1 error
tells me there's nothing untoward in the classpath.
The first, and only definition (if EditPlus's find-in-files is to be believed) is in C:\Program Files\Java\jdk1.6.0\jre\lib\rt.jar ... which I presume is the java run time library.
7Zip tells me there is indeed a SwingWorker.class in there... so I extract ot to c:\tmp and decompile the sucker...C:\tmp>javap -classpath . SwingWorker
Compiled from "SwingWorker.java"
public abstract class javax.swing.SwingWorker extends java.lang.Object implements java.util.concurrent.RunnableFuture{
public javax.swing.SwingWorker();
protected abstract java.lang.Object doInBackground()throws java.lang.Exception;
public final void run();
protected final void publish(java.lang.Object[]);
protected void process(java.lang.Object[]);
protected void done();
protected final void setProgress(int);
public final int getProgress();
public final void execute();
public final boolean cancel(boolean);
public final boolean isCancelled();
public final boolean isDone();
public final java.lang.Object get()throws java.lang.InterruptedException, java.util.concurrent.ExecutionException;
public final java.lang.Object get(long, java.util.concurrent.TimeUnit)throws java.lang.InterruptedException, java.util.co
ncurrent.ExecutionException, java.util.concurrent.TimeoutException;
public final void addPropertyChangeListener(java.beans.PropertyChangeListener);
public final void removePropertyChangeListener(java.beans.PropertyChangeListener);
public final void firePropertyChange(java.lang.String, java.lang.Object, java.lang.Object);
public final java.beans.PropertyChangeSupport getPropertyChangeSupport();
public final javax.swing.SwingWorker$StateValue getState();
static void access$000(javax.swing.SwingWorker, javax.swing.SwingWorker$StateValue);
static void access$100(javax.swing.SwingWorker);
}
which tells me process takes an array of objects...protected void process(java.lang.Object[]);
which I believe is not exactly the same as the API docoprotected void process(List<V> chunks)
... but I'd readily believe that that's what it's actually compiled into, or that Object[] is the best approximation that javap can make of a List.
So how come it works on saber's box and not mine... maybe I need to get the latest beta... I've got "Java(TM) SE Runtime Environment (build 1.6.0-beta2-b86)"
Maybe Sun's java.exe has detected the presence of a Microsoft operating system, and said this guy doesn't need a compiler, he needs a labotomy.
Any help would be greatly appreciated.
Keith.
> I've got "Java(TM) SE Runtime Environment (build 1.6.0-beta2-b86)"No wonder! That's yonks old! There is a released 1.6 - http://java.sun.com/javase/downloads/index.jsp .
Thanx saber... I was starting to go mad...Is "Java EE 5 SDK Update 2" the latest J2EE SDK? ... the 5 is throwing me.
"Java EE 5 SDK Update 2" isn't JDK... I kind of presumed that EE SDK came with a SE JDK... but apparently it does not.
I just got "JDK 6" from http://java.sun.com/javase/downloads/index.jsp, installed it and whalla... no more @Override problems.
- compile -
Output completed (0 sec consumed) - Normal Termination
Thank You Mr Saber Sir!