Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Java Programming 24-Hour Trainer by Yakov Fain
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Java Programming 24-Hour Trainer by Yakov Fain
This is the forum to discuss the Wrox book Java Programming 24-Hour Trainer by Yakov Fain; ISBN: 978-0-470-88964-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Java Programming 24-Hour Trainer by Yakov Fain section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 24th, 2012, 06:08 AM
Authorized User
Points: 159, Level: 3
Points: 159, Level: 3 Points: 159, Level: 3 Points: 159, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Location: Romania
Posts: 30
Thanks: 5
Thanked 4 Times in 4 Posts
Default Listing 21-6: A typical way to use SwingWorker

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.
Reply With Quote
  #2 (permalink)  
Old February 24th, 2012, 08:23 AM
Registered User
Points: 27, Level: 1
Points: 27, Level: 1 Points: 27, Level: 1 Points: 27, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Location: New Delhi
Posts: 5
Thanks: 0
Thanked 1 Time in 1 Post
Smile Error Cause

You are getting that error because that method
Code:
protected void process(String progressMessage){
	        // display the progress information here
	    	//System.out.println(progressMessage);
	    }
is not same as in its Super Class
correct method is
Code:
protected void process(List<T> list){
	  
	    }
so you can use this code instead of your code

Code:
protected void process(List<String> progressMessage){
	        // display the progress information here
	    	//System.out.println(progressMessage);
	    }
Hope it'll work better
Reply With Quote
The Following User Says Thank You to jitendrabisht For This Useful Post:
Cristina (February 24th, 2012)
  #3 (permalink)  
Old March 15th, 2012, 07:20 AM
Authorized User
Points: 159, Level: 3
Points: 159, Level: 3 Points: 159, Level: 3 Points: 159, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Location: Romania
Posts: 30
Thanks: 5
Thanked 4 Times in 4 Posts
Default Lesson 21 Try It

Feel so good after completing Lesson 21 assignment
Many thanks for your help!

To populate a Swing view from a file, I found the following link useful:
http://stackoverflow.com/questions/3...ents-of-a-file

Here's my solution:
Code:
public class NewsReaderFrame {
	
	public JTextArea text1;
	public JTextArea text2;
	
	NewsReaderFrame(){
		JPanel textPanel = new JPanel();
		text1 = new JTextArea(30,30);
	    JScrollPane scrollPane1 = new JScrollPane(text1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
		        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	    
	    text2 = new JTextArea(30,50);
	    JScrollPane scrollPane2 = new JScrollPane(text2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
	        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
 
		textPanel.add(scrollPane1);
		textPanel.add(scrollPane2);
		
		JPanel buttonPanel = new JPanel();
 		buttonPanel.setLayout(new GridLayout(1,1));
		JButton button = new JButton("Read News");
		button.addActionListener(this.new NewsReaderButtonAction());
		
		buttonPanel.add(button);
		
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
		
		panel.add(textPanel);
		panel.add(buttonPanel);
		
		JFrame frame = new JFrame("Lesson21: File Reader With SwingWorker");
		
		frame.setContentPane(panel);
		frame.setLocation(100,50);
		frame.setVisible(true);
		frame.pack();
	}
	
	public static void main(String[] args){
				
		NewsReaderFrame nrf = new NewsReaderFrame();
								
	}
	
	class NewsReaderButtonAction implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			
			System.out.println("Read News: Button Pressed");
			
			File file1 = new File("C:\\Users\\Cristina\\Documents\\raspuns.txt");
			File file2 = new File("C:\\Users\\Cristina\\Documents\\ResearchStatement.tex");
			
			NewsReader nr1 = new NewsReader(file1, text1);
			NewsReader nr2 = new NewsReader(file2, text2);
 
			nr1.execute();
			nr2.execute();

	}
	}	
}
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Typical Algorithm rajanikrishna ASP.NET 1.0 and 1.1 Professional 0 November 20th, 2006 03:05 AM
Displaying null value in a typical combobox - C# momof2 C# 1 August 24th, 2006 12:47 AM
typical logic rajanikrishna Classic ASP Basics 2 June 11th, 2004 11:15 PM
A Typical Book Class Maxood ASP.NET 1.0 and 1.1 Basics 8 March 23rd, 2004 04:57 PM



All times are GMT -4. The time now is 10:57 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.