51

I am a newbie to Java. I am trying to dynamically choose the file location to save the outcome of my project (to be initiated at the very start of my project). I worked around with a few FileDialog examples, but each one of them allows me to choose a file and not a folder.

Can anyone please help me with an example (or) link to one for the same?

1
  • 1
    Ok, this is a good start. Sounds like you've done some research. Can you include some examples of what you tried? That will help others work with what you already know. Commented Apr 10, 2012 at 5:26

5 Answers 5

89

You could try something like this (as shown here: Select a Directory with a JFileChooser):

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;


public class DemoJFileChooser extends JPanel
   implements ActionListener {
   JButton go;

   JFileChooser chooser;
   String choosertitle;

  public DemoJFileChooser() {
    go = new JButton("Do it");
    go.addActionListener(this);
    add(go);
   }

  public void actionPerformed(ActionEvent e) {            
    chooser = new JFileChooser(); 
    chooser.setCurrentDirectory(new java.io.File("."));
    chooser.setDialogTitle(choosertitle);
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    //
    // disable the "All files" option.
    //
    chooser.setAcceptAllFileFilterUsed(false);
    //    
    if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 
      System.out.println("getCurrentDirectory(): " 
         +  chooser.getCurrentDirectory());
      System.out.println("getSelectedFile() : " 
         +  chooser.getSelectedFile());
      }
    else {
      System.out.println("No Selection ");
      }
     }

  public Dimension getPreferredSize(){
    return new Dimension(200, 200);
    }

  public static void main(String s[]) {
    JFrame frame = new JFrame("");
    DemoJFileChooser panel = new DemoJFileChooser();
    frame.addWindowListener(
      new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
          }
        }
      );
    frame.getContentPane().add(panel,"Center");
    frame.setSize(panel.getPreferredSize());
    frame.setVisible(true);
    }
}
5
  • Thanks a lot Guys... Does Exactly what i needed.. Wanted to do something with the "Usefulness of this answer" points for u.. But apparently i need 15 reputation points :(
    – Sam
    Commented Apr 10, 2012 at 5:35
  • @Sam: Usually ticking the green 'very good' mark under the answer score does the trick.
    – npinti
    Commented Apr 10, 2012 at 8:09
  • What is int result used for? Commented Apr 26, 2015 at 18:54
  • The chooser.showOpenDialog(this) return an integer which denotes the user's response according to an enumeration. It would seem that the intended purpose for int result was to hold this number. I'll remove it to avoid confusion.
    – npinti
    Commented Apr 27, 2015 at 6:19
  • 3
    keep in mind that after returning from dialog the method getSelectedDirectory() returns the directory in which the selected directory is. getSelectedFile() returns the actual selected directory\ Commented Mar 4, 2016 at 10:48
40

Oracles Java Tutorial for File Choosers: http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html

Note getSelectedFile() returns the selected folder, despite the name. getCurrentDirectory() returns the directory of the selected folder.

import javax.swing.*;

public class Example
{
    public static void main(String[] args)
    {
        JFileChooser f = new JFileChooser();
        f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
        f.showSaveDialog(null);

        System.out.println(f.getCurrentDirectory());
        System.out.println(f.getSelectedFile());
    }      
}
1
  • 3
    Thumbs up for describing difference between getCurrentDirectory getSelectedFile.
    – Tinus Tate
    Commented Jan 4, 2019 at 14:38
16

try something like this

JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("select folder");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
3
  • Thanks a lot.. Let me Start experimenting in this direction
    – Sam
    Commented Apr 10, 2012 at 5:33
  • What's the point of "chooser.setCurrentDirectory(new java.io.File("."));" line. i really don't get it
    – cssGEEK
    Commented Aug 6, 2015 at 18:12
  • 4
    @cssGEEK maybe too late to answer, but it makes it so that the dialog starts at the current directory. In Unix (at least), every directory has a folder called . and a folder called ... The first being a reference to the same directory, and the latter being the parent directory. In Java, the String you give to the constructor of File is a relative path if it does not start with a / (Unix) or `X:` (Windows), so if you give it ".", it is a folder relative to where the current directory of the program. That will make the chooser point to the current directory where the program is running. Commented Jul 24, 2016 at 8:13
3

Along with JFileChooser is possible use this:

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

for have a Look and Feel like Windows.

for others settings, view here: https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html#available

2

I found a good example of what you need in this link.

import javax.swing.JFileChooser;

public class Main {
  public static void main(String s[]) {
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new java.io.File("."));
    chooser.setDialogTitle("choosertitle");
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setAcceptAllFileFilterUsed(false);

    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
      System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
      System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
    } else {
      System.out.println("No Selection ");
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.