Swingworker with FileVisitor class and walkFileTree(), which iterates internally over a “set” of files in a directory tree; where to publish()?

前端 未结 3 1564
萌比男神i
萌比男神i 2021-01-29 02:15

It seems like the long-running tree walker task should be defined in a class like so:

  public class TreeWalker extends SwingWorker implements         


        
3条回答
  •  一生所求
    2021-01-29 02:55

    It's not that I gave up on SwingWorker, I just determined that I didn't know squat about threads and decided to do something about it. My success the past 2 days with a much simpler project led me to apply that same strategy to my (various) Treewalker (projects), which now: (1) do not make the screen flash when appending output to a textarea and (2) end gracefully and immediately with a buttonpress.

    All it took was using a separate thread (not a SwingWorker) for the "background" FileVisitor task, which: (a) let GUI stay "in charge" and thus be able to accept output seamlessly as well as provide a button for the user to press to abort and (b) makes the code look sane and easy to follow.

    So @Mad, thanks AGAIN for the help. (I haven't been working solely on this since Nov. 19! I got so frustrated I just left it, did other stuff successfully, and got the nerve to come back and try again).

    P.S. I found the text Ivar Horton's Beginning Java (7) invaluable. Best I've seen about threads.

    FWIW here's an outline of my backup program:

    public class Copy extends Thread{
    
      public static FileVisitResult disposition = FileVisitResult.CONTINUE;
      static Thread           t ;
      static FilesCopied      output ;   
    ...
      public static TreeWalker      fv;
    ...
    
      public void run() {    
    ...
        fv             = new TreeWalker();  
        try {
          Files.walkFileTree(UserIO.inputPath,fv);
        }
        catch ...
      }
    
      public /* inner */ class TreeWalker implements FileVisitor {
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
          maybeCopy(file);
          return disposition;
        }
        public FileVisitResult preVisitDirectory(Path d, BasicFileAttributes a) throws IOException {
          maybeMakeDir(d,fromRootDepth);
          return disposition;     
        }
    ... 
      } // end TreeWalker
    
    ...
      public static void main(String[] args) throws IOException {
        EventQueue.invokeLater(new Runnable()  
          public void run() { gui = new UserIO(); gui.setVisible(true); 
        }});  
        EventQueue.invokeLater(new Runnable() {
          public void run() {
            output = new FilesCopied();
          }});
        t = new Copy();
      }
    } // end class Copy
    ======================
    public class UserIO extends JFrame {
    ...
      public void btnBackupMouseClicked(MouseEvent evt) throws IOException { 
    ...
          Copy.t.start();
      }
    
      public void btnStopMouseClicked(MouseEvent evt) throws IOException { 
        Copy.disposition = FileVisitResult.TERMINATE;
      }                                      
    }
    

提交回复
热议问题