48

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?

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
Sam
  • 595
  • 1
  • 4
  • 7
  • 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. – jamesmortensen Apr 10 '12 at 05:26

5 Answers5

88

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);
    }
}
npinti
  • 51,780
  • 5
  • 72
  • 96
  • 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 Apr 10 '12 at 05:35
  • @Sam: Usually ticking the green 'very good' mark under the answer score does the trick. – npinti Apr 10 '12 at 08:09
  • What is `int result` used for? – user2084795 Apr 26 '15 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 Apr 27 '15 at 06:19
  • 2
    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\ – Developer Marius Žilėnas Mar 04 '16 at 10:48
38

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());
    }      
}
jmw
  • 381
  • 3
  • 3
15

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);
npinti
  • 51,780
  • 5
  • 72
  • 96
Nagaraju Badaeni
  • 890
  • 8
  • 14
  • Thanks a lot.. Let me Start experimenting in this direction – Sam Apr 10 '12 at 05:33
  • What's the point of "chooser.setCurrentDirectory(new java.io.File("."));" line. i really don't get it – cssGEEK Aug 06 '15 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. – Aritz Lopez Jul 24 '16 at 08: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 ");
    }
  }
}