Can't override process() method in SwingWorker
Asked Answered
P

3

6

I have a SwingWorker class as follows:

    class RemotePlayersWorker extends SwingWorker<String[][], Object> {
        PlayerCanvas parent;
        RemoteHandler remote;
        String[][] players;
        int maximumConnections;

        public RemotePlayersWorker(PlayerCanvas parentCanvas, RemoteHandler remoteHandle) {
            this.parent = parentCanvas;
            this.remote = remoteHandle;
        }

        @Override
        protected String[][] doInBackground() throws Exception {
            System.out.println("TEST 1");
            players = remote.getConnectedPlayers();
            publish(players);
            return players;
        }

        @Override
        protected void process(List<String[][]> chunks) {
            for (String[][] chunk : chunks) {
                 // no need for the c variable
                 System.out.println(chunk.toString());
              }
        }

        @Override 
        protected void done() {

        }
    }

However, I'm getting errors when overriding the process(List chunks) method. Eclipse tells me this:

The method process(List) of type PlayerHandler.RemotePlayersWorker must override or implement a supertype method

However, as far as I can tell, I am overriding the method correctly - I get the same error regardless of what I set the list type to.

Is there any other reason I wouldn't be able to override process()?

I'm using java version "1.7.0_10" - Java(TM) SE Runtime Environment (build 1.7.0_10-b18)

Phosphine answered 17/3, 2013 at 9:9 Comment(0)
L
6

The SwingWorker class is defined as follows:

public class SwingWorker<T, V> {
    ...
    protected void process(List<V> chunks) {
        ...
    }
}

So, since your subclass is declared as

class RemotePlayersWorker extends SwingWorker<String[][], Object> {

The process method should take a List<Object> as argument, and not a List<String[][]>

Lorileelorilyn answered 17/3, 2013 at 9:15 Comment(0)
O
4

You have incorrect parameter in your process() method it should be

protected void process(List<Object> chunks) {
   /// do your stuff
}

It takes 2nd. generic datatype of SwingWorker class. Please read documentation.

Explanation:

public class SwingWorker<T, V> {

    // methods

    protected void process(List<V> chunks) {
       // do your stuff
    }
}

Here you can see more purely that process() method takes V that is in your case Object.

So either change it to List<Object> or reverse your signature of SwingWorker class(but then you need to change return type of doInBackground() method).

Ohm answered 17/3, 2013 at 9:15 Comment(0)
N
3

Your siganture for the process method is wrong.

The signature from SwingWorker : protected void process(List<V> chunks)

V - the type used for carrying out intermediate results by this SwingWorker's publish and process methods

So it should be :

protected  void process(List<Object> chunks)
Naturalist answered 17/3, 2013 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.