Jatin Pandav
Jatin Pandav

Reputation: 11

Modification to JFileChooser to just display folder content

i m trying to develop an gui application which display tree of filesystem on left side and on right side it display selected tree nodes (folder)'s content . can any body tell me to do modification in jfilechooser to just to display folder content thank you in advance

Upvotes: 0

Views: 1066

Answers (3)

gkris
gkris

Reputation: 1219

Perhaps this is what you expected:-

JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

try {
   // Create a File object containing the canonical path of the desired directory
   File f = new File(new File(".").getCanonicalPath());

   // Set the current directory
   chooser.setCurrentDirectory(f);
} catch (IOException e1) {
   e1.printStackTrace();
}

// Show the dialog; wait until dialog is closed
int returnVal = chooser.showOpenDialog(frame);

if(returnVal == JFileChooser.APPROVE_OPTION)
{
    File f = chooser.getSelectedFile();
    textField.setText(f.getAbsolutePath());

    File[] contents = f.listFiles();
    for(int file=0;file<contents.length;file++)
    {
        System.out.println(contents[file].getName()); //here you get the contents of the selected directory
    }
}

Upvotes: 0

cdc
cdc

Reputation: 2571

See: example

JFileChooser fileChooser = new JFileChooser();

fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

Upvotes: 0

Robin
Robin

Reputation: 36611

The JFileChooser#accept allows you to filter which files are displayed. A similar method is the JFileChooser#setFileFilter method

Upvotes: 1

Related Questions