52

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

90

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); } } 
Sign up to request clarification or add additional context in comments.

5 Comments

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: Usually ticking the green 'very good' mark under the answer score does the trick.
What is int result used for?
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.
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\
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 Comment

Thumbs up for describing difference between getCurrentDirectory getSelectedFile.
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 Comments

Thanks a lot.. Let me Start experimenting in this direction
What's the point of "chooser.setCurrentDirectory(new java.io.File("."));" line. i really don't get it
@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.
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

Comments

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 "); } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.