Reputation: 4283
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
.
Upvotes: 0
Views: 126
Reputation: 4283
Thanks to @Hover, I have a reasonably-smooth-working Search program. Highlights (i.e., no details about variables or logic):
public class Main
{
public static void main(String args[])
{
EventQueue.invokeLater
(
new Runnable()
{
@Override
public void run()
{
gui = new GUI();
}
}
);
}
}
//==============================================================
public class GUI extends JFrame
{
private static void btnSearchActionPerformed(ActionEvent evt)
{
TASK task = new TASK();
task.execute();
}
}
//==============================================================
public class TASK extends SwingWorker<Void,String>
{
FV fv;
TASK()
{
fv = new FV();
}
//-------------- inner class
class FV implements FileVisitor<Path>
{
public FileVisitResult visitFile(Path f, BasicFileAttributes a) throws IOException
{
if(f.getFileName().toString().toLowerCase().matches(fPatt.toLowerCase().trim()))
{
publish(s); // old --> report(f); -- see process method below
}
return Main.disposition;
}
}
//----------------
protected Void doInBackground() throws Exception
{
Files.walkFileTree(path, fv);
return null;
}
// **EDIT** added new method
@Override
protected void process(List<String> chunks){
for (int i = 0; i < chunks.size(); i++) {
report(chunks.get(i));
}
}
}
Only thing troubling me is that I haven't implemented the publish
and process
and other SwingWorker
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 is Void
above, must be the type returned by doInBackground
(and get
, if used).
The second, String
above, must be the type used in publish
and process
.
Upvotes: 0