Dear all,
I have tried to complete the code in Listing 21-6 just to eliminate the compilation errors and get a workable program, even though too simple to illustrate multithreading, as follows:
Code:
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import java.util.ArrayList;
import java.util.List;
class MarketNewsWorker extends SwingWorker <List<String>, String>{
ArrayList<String> someNewsCollection;
MarketNewsWorker(){
someNewsCollection=new ArrayList<String>();
someNewsCollection.add("Some");
someNewsCollection.add("news");
someNewsCollection.add("collection");
}
@Override public List<String> doInBackground(){
// Make a request to the server and return a result,
// i.e. a list of Strings
ArrayList<String> myListOfTextData = new ArrayList<String>();
for (String news: someNewsCollection){
//process each news and report the progress
myListOfTextData.add(news);
publish("Processed the news " + news); //this calls process()
}
return myListOfTextData;
}
@Override protected void process(String progressMessage){
// display the progress information here
//System.out.println(progressMessage);
}
@Override protected void done(){
// modify UI components here by calling get()
// Future's get() gives you the result of
// the thread execution
try{
System.out.println(get());
}
catch(Exception e){e.printStackTrace();}
}
}
class TestMarketNews{
public static void main(String[] args){
new MarketNewsWorker().execute();
JOptionPane.showMessageDialog(null, "Bye!");
}
}
I get an error at the line I underlined in red, saying "The method process(String) of type MarketNewsWorker must override or implement a supertype method" and suggesting to remove the @Override annotation.
I know that here the annotation is not necessary and used just as an example, but still I would like to understand the error message and why I get it only for the method process(), and not for doInBackground() and done(), where I use the same annotation.