1

I have revised my Windows 7 search program to write matched files to a JTable. Prior to this I was writing to a JTextArea. The output was ugly; hence JTable output. But with JTable, the program becomes unresponsive from time to time if searching thousands of files. (Not so with JTextArea. Very smooth.)

My question is how to improve responsiveness.

I have 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 class header for TASK:

public class TASK extends SimpleFileVisitor<Path> implements Runnable{

Here's run:

  public void run() 
  {
      SearchyGUI.disposition = FileVisitResult.CONTINUE;
      Files.walkFileTree(path , this);
  }

Here's the main routine for tree walking (there are 3 others, one for processing directories, one for post-processing them, and one for errors):

  public FileVisitResult visitFile(Path f, BasicFileAttributes a) throws IOException {
    if(a.isSymbolicLink())
      return SearchyGUI.disposition;
    File file = new File(f.getParent().toString(), f.getFileName().toString());
    try 
    {
      if(f.getFileName().toString().toLowerCase().matches(fPatt.toLowerCase().trim())
          report(s);
    } catch (MalformedURLException | SAXException | TikaException ex) {msgbox(ex.toString());}
    return SearchyGUI.disposition;
  }

Matched files and their info are written to a JTable:

      private static void report(...){
        ArrayList<String> rowData = new ArrayList<>(4);
              rowData.add(date);
              rowData.add(size);
              rowData.add(filename);    
              rowData.add(path);
              dftTableModel.addRow(new Object[]{rowData.get(0),rowData.get(1),rowData.get(2),rowData.get(3)});
          }
          catch (Exception e)
          { 
            SearchyGUI.disposition = FileVisitResult.TERMINATE;
          }
      }

To improve responsiveness, I don't know if I need another Thread for writing to the JTable or if I need to use a SwingWorker. In either case, I'm not sure how to go about implementation.

Assuming another Thread, would I turn the report method into a class that implements Runnable? I'm having trouble visualizing implementation but I can probably figure it out if I have to.

I once used SwingWorker, probably incorrectly (with no Thread), with this program. I gave it up when the output area flashed incessantly. So I'm not keen on going that route, but I will figure it out if advisable.

I don't think the question falls in the "opinion" area. Need advice on how to improve responsiveness. I just don't want to head down a wrong path if someone can point the error in doing so in advance.

enter image description here

DSlomer64
  • 4,234
  • 4
  • 53
  • 88
  • 2
    `I once used SwingWorker, probably incorrectly (with no Thread), with this program` - the Swing worker is a separate Thread automatically and is probably your best approach. All you need to do is "publish" results as they become available and you won't have response problems. Read the Swing tutorial on `Concurrency` for more information about the SwingWorker and publishing results. ` I gave it up when the output area flashed incessantly.` - there is no reason for it to flash. Again, start with a simple example so you understand the concept, then apply the knowledge to your real application. – camickr Aug 16 '15 at 21:18
  • 3
    `"To improve responsiveness, I don't know if I need another Thread for writing to the JTable"` -- no, not *another* Thread -- you must write to the table model in the Swing event thread or EDT. `"... or if I need to use a SwingWorker."` -- this is a useful tool that allows you to pass information from a background thread into the event thread. Go through the tutorial on this if you don't know how to use it. – Hovercraft Full Of Eels Aug 16 '15 at 21:20
  • 2
    [Lesson: Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) – Hovercraft Full Of Eels Aug 16 '15 at 21:23
  • 1
    `SwingWorker` it is. Good point about writing to EDT. Forgot that important principle. THANKS, BOTH O' YA! – DSlomer64 Aug 16 '15 at 21:23
  • 1
    For [example](http://stackoverflow.com/a/25526869/230513). – trashgod Aug 16 '15 at 22:31

0 Answers0