CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2008
    Posts
    32

    Setting up Swing worker

    I'm trying to set up a swing worker that does some file processing in the background while updating progress to a JtextArea.



    From class X I call swingworker FileProcessor
    Code:
    {
    JTextArea outerTextArea;
    File selectedFile
    
    FileProcessor task = new FileProcessor(selectedFile, outerTextArea);
    selectedFileHolder = task.execute();
    }
    Fileprocessor Internals


    Code:
    class FileProcessor extends 
             SwingWorker<File, JTextArea> {
    	
        
        
         FileProcessor(File INPUT, JTextArea innerTextArea) { 
             //initialize 
         }
    
    
    @Override
    public File doInBackground() {
      	File convertedFile = new File("convertedFile.txt");
    	int counter=0;
    	
    	IoUtil.assertFileIsReadable(INPUT);
    
        
        final GenericFileReader reader = new GenericFileReader(INPUT);
    
    while(somecondition==condition)
    {
    //processing stuff
    counter++;
    }
    
    return convertedFile;
    
      }
    }
    A few questions

    It keeps telling me INPUT isn't recognized in the FileProcessor class.

    How do I get convertedFile in FileProcessor to be selectedFileHolder in Class X?

    How would I get counter in FileProcessor to constantly update to outerTextArea in class X?

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Setting up Swing worker

    It keeps telling me INPUT isn't recognized in the FileProcessor class.
    Please use Java naming conventions ie don't use all upper case letters for variable names unless they are constants.

    The problem is INPUT is a parameter in the doInBackground classes constructor and not a instance variable and so it is not visible in the doInBackground() method. In the constructor assign INPUT to an instance variable and use that to refer to the file.

    How would I get counter in FileProcessor to constantly update to outerTextArea in class X?
    You provide an implementation of the process(..) method to update the text area and call publish(..) from the doInBackground() to pass the data to display to the process() method.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured