Solved

Searching & Setting Directories in Java

Posted on 2003-12-08
16
303 Views
Last Modified: 2010-03-31
Hello i am producing a java program and in my program it involves chosing a directory to search for different file types.  Currently, i have created the code to search for a file, now i just need to edit this code in order to set a directory in which to search.

public class directory extends JFrame implements ActionListener
{
FileDialog f = new FileDialog(this, "Choose A Folder To Search For Files", FileDialog.LOAD); // Loads the file select box
}

public void actionPerformed(ActionEvent e)
{
 if(e.getSource() == rootDir){
f.setDirectory("M:\\Year 2");       // If root directory is selected go here
f.setVisible(true);
 }

So all i basiclly need to do is to select a dirctoty and subdirectories not files.

Thanks Ian.
0
Comment
Question by:ian_willo
  • 7
  • 5
  • 4
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9896703
set the FileDialog.DIRECTORIES_ONLY flag
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9896711
>> select a dirctoty

You could get the selected directory (in the FileDialog) into a File object using:

File selectedDir = f.getDirectory();

listFiles() will give you a list of files and directories from a given directory.

You can then use isDirectory() on each entry to see if it is a directory or not.
0
 

Author Comment

by:ian_willo
ID: 9896713
What do you mean.  how do i implement that in my code??????????
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 15

Expert Comment

by:jimmack
ID: 9896727
Who are you asking?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9896734
You should stick to Swing if you're generally using Swing:

JFileChooser fileChooser = new JFileChooser(file);
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
0
 

Author Comment

by:ian_willo
ID: 9896738
either of you i dont understand what you mean. Only been doing java for about 1 and a half months
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9896744
Here are some useful chooser examples

http://javaalmanac.com/egs/javax.swing.filechooser/pkg.html
0
 

Author Comment

by:ian_willo
ID: 9896760
CEHJ where do i put this code mate
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9896784
When you have selected the directory from either the JFileChooser or FileDialog, you want a File object.  From a JFileChooser, you can call getSelectedFile(), from FileDialog, you can call getDirectory(), but this only returns a String:

JFileChooser (let's call it jfc):

File selectedDir = jfc.getSelectedFile();

FileDialog (from your example - f)

File selectedDir = new File(f.getDirectory());

Now you have the File you can get the files/directories that it contains.  You can either create a filter for this method (to only return directories), or you can search the returned array for directories:

File[] dirContent = selectedDir.listFiles();

for (int i = 0; i < dirContent.length; i++)
{
    if (dirContent[i].isDirectory())
    {
    }
    else  // must be a file
    {
    }
}
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 9896796
Wherever you want to show the dialog, e.g.

void showDialog() {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
     
    int returnVal = chooser.showOpenDialog(this);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
       System.out.println("You chose to open this directory: " +
            chooser.getSelectedFile().getName());
    }

}
0
 

Author Comment

by:ian_willo
ID: 9896799
let me explain a bit more.  I have two buutons called movies and pictures.  Also a root dircetory button.  I want to click the root dir button to set the directory.  Then click the movies button to display mpgs and the pictures to display bmps.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9896816
Just call my code from actionPerformed. The API docs show how to set filters:

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFileChooser.html
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9896819
Does the root dir button open the file choosing dialog?

Where are the other buttons?

You might want to consider closing your old question: http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20776870.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9896826
If you want to set a root, create the chooser with a String representing where the root is (see API doc)
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9896885
And here is an example of a FileFilter that the listFiles method can use to retrieve specified files from a directory:

private class MyFilter implements FileFilter
{
    private String suffix;

    public boolean accept(File pathname)
    {
        return (pathname.getName().endsWith(suffix));
    }

    public MyFilter(String suffix)
    {
        this.suffix = suffix;
    }
}

You could then get the .mpg files in a directory using:

File[] mgpFiles = currentDirectory.listFiles(new MyFilter("mpg"));
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9911562
8-)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 86
JDeveloper 12c for 32 bit 4 85
windows explorer path to command prompt 5 45
difference of if loops 23 49
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question