File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Hard Drive Search Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Hard Drive Search" Watch "Hard Drive Search" New topic

Hard Drive Search

Eibhlin Reid

Joined: Nov 04, 2004
Posts: 28
I am doing a project which performs a search on the C drive of a machine. I would like to display the name of the file currently being searched. Anybody know how I could do this?? I presume that I will need some sort of listener to get the name to change as the file being searched changes, I'm not sure how to do this though.

Thanks in advance!
Jason Kingsley

Joined: Feb 21, 2004
Posts: 18
Hi there,

This is relatively simple, but first of all, do your long running task in a separate thread to the swing ui thread otherwise you have a very unresponsive and broken ui.

So, assuming you have a nice worker thread and an algorithm for traversing your disk, a simple one being (in pseudo code)

SearchFile(target directory):

1) from target director

2) for each file in target directory
3) search file

4) for each folder in target directory
5) SearchFile(selected child folder)

This is obviously a depth first recursive search and if you have an extremely deep directory structure you may run out of stack space - but it will do.

What this illustrates though is that you have a handle onto the file that is being searched in step 3). So quite simply, from that step, use what ever notification method you desire to notify some ui component/form that a file is being searched. This could be either having a "filesearched" custom event (better) or a handle onto the component/form and update some text (worse as its tightly coupled).

Remember though that if you update a component from a worker thread (and not the swing ui thread) then you will need to marshal the call across onto the swing ui thread with invokeLater. Do a search on Google for SwingWorkerThread and you'll find out more.
[ April 05, 2005: Message edited by: Jason Kingsley ]
I agree. Here's the link:
subject: Hard Drive Search
It's not a secret anymore!