This continues this thread of August 16, 2015
I'm modifying my Search
program to use SwingWorker
. The original code (main
shown below) has one Thread
that is invoked in main
and "walks the file tree" from a given node:
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
gui = new GUI();
Utilities.disable(GUI.btnStop);
}});
t = new Thread(new TASK());
taskStarted = false;
}
}
Here's the original class header for TASK
:
public class TASK extends SimpleFileVisitor<Path> implements Runnable{
But in order to use SwingWorker
, (I assume) I need TASK extends SwingWorker
, making the original run
command illegal (error: run
is final
in SwingWorker
).
Here's original run
:
public void run()
{
SearchyGUI.disposition = FileVisitResult.CONTINUE;
Files.walkFileTree(path , this);
}
Since I can't use run
, I made the code above the initialization for class TASK
like so:
public class TASK implements SwingWorker implements FileVisitor<Path> {
public void TASK()
{
System.out.println("Here we are starting TASK...");
SearchyGUI.disposition = FileVisitResult.CONTINUE;
Files.walkFileTree(path , this);
}
But now the line in main
below causes error (no suitable constructor since now TASK doesn't implement runnable....):
t = new Thread(new TASK());
And if I just say new TASK();
the GUI shows, but when I click the Search button, nothing happens. No file walk. No errors. The output from TASK
doesn't even show. So no chance to invoke SwingWorker
. (In fact, just to see what might happen [nothing] I removed it from the class header for TASK
: public class TASK /*extends SwingWorker*/ implements FileVisitor<Path>
.)
If anything obvious is wrong, I'd love to see it. If not, I will spend a good long while making a SSCCE
.
Thanks to @Hover, I have a reasonably-smooth-working Search program. Highlights (i.e., no details about variables or logic):
Only thing troubling me is that I haven't implemented the
publish
andprocess
and otherSwingWorker
methods.EDIT
So I included the two in the code above.
EDIT 2
I changed types on several statements, based on the class definition for
TASK
:public class TASK extends SwingWorker<Void,String>
The first generic type parameter for
SwingWorker
, which isVoid
above, must be the type returned bydoInBackground
(andget
, if used).The second,
String
above, must be the type used inpublish
andprocess
.