It seems like the long-running tree walker task should be defined in a class like so:
public class TreeWalker extends SwingWorker<Void,String> implements FileVisitor<Path>
And begun somewhere like so:
TreeWalker walker = (new TreeWalker());
walker.execute();
The long-running task is not only initiated by but completely carried out by a single call to walkFileTree()
, a method in the Files
class. So surely the call to it has to be in doInBackGround()
.
protected Void doInBackground() throws Exception {
Files.walkFileTree(SearchyGUI.p , this);
return null;
}
Note that walkTreeFile()
internally calls four methods for each file encountered. A programmer-written loop isn't feasible. So there's my problem. How do I use publish()
to send file information as a string to the process
method I need to override? Examples I've seen have publish()
inside doInBackground()
, but inside a loop, which is not possible here.
The one of the four methods that I care most about is visitFile()
, which walkFileTree()
needs to be able to find and I suspect this is where to put publish()
:
public FileVisitResult visitFile(Path f, BasicFileAttributes a) throws IOException {
if (...we want this file...)
publish(f.toString());
return CONTINUE;
}
I could put all 4 methods that walkFileTree() calls in an inner class inside doInBackground()
, but that seems like wishful thinking.
P.S. I can't use get()
; that's the whole point (as I understand it)--too much delay in getting results (may process thousands of files to find a dozen) to wait until doInBackground() ends.
==========================================
EDIT #3, 50 minutes after original post time
public static void doIt(){
try {
System.out.println("It begins..."); // This does happen.
TreeWalker walker = new TreeWalker();
walker.execute();
SearchyGUI.info.setVisible(true); // Form is displayed, stays blank.
}
catch (Exception e) { System.out.println("Uh-oh"); } // This does NOT happen.
}
==========================================
(EDIT #2, 40 minutes after post)
Here's my process method. The println didn't execute.
protected void process(String s) {
System.out.println("in process()...");
report(s); // my method to append text area with another line of file info
}
Also, the class statement that contains doInBackground()
has changed:
public class TreeWalker extends SwingWorker<Void, String> implements Runnable{
The Walking
class is nested within doInBackground()
.
==========================================
(EDIT, 20 minutes after post)
This compiled but did nothing:
protected Void doInBackground() throws Exception
{
class Walking implements FileVisitor<Path>
{
@Override
public FileVisitResult visitFile(Path f, BasicFileAttributes a) throws IOException
{
String modifyDate = a.lastModifiedTime().toString().substring(0,10);
String fpathname = f.toString();// + "\\" + f.getFileName().toString());
if (...we want this one...)
publish(f.getFileName());
return disposition;
}
... other methods excluded
} // end inner class
System.out.println("walking??"); // We get here ...
Files.walkFileTree(SearchyGUI.p , (FileVisitor<? super Path>) this);
System.out.println("Finished walking??"); // ... but not here.
return null;
} // end of doInBackground()
=============================
... another freakin' edit ... my current class defs...
public class GUI extends JFrame implements ActionListener, MouseListener, KeyListener
public class TreeWalker extends SwingWorker<Void, String> implements Runnable{
protected Void doInBackground() throws Exception {
class Walking implements FileVisitor<Path>{ // CLASS INSIDE doInBackground
... zzzzzzzzzzzzzzzzzzz ...........
Because your
TreeWalker
extends bothSwingWorker
and implementsFileVisitor
, you could be able to callpublish
from within any of the call back methods, for example...Now, based on your needs, you would need to convert the
Path
element to aString
using what ever method you need...Updated with working example
Nb, this doesn't have a GUI with it, but instead waits for the worker to complete by waiting for
get
to return is only meant as an example