Sri Sri
Sri Sri

Reputation: 3109

Browse for folder dialog

I need to know how to get the "browse for folder" dialog in java. I am aware of SWT. But I need to do in swing? Is there any solution to this?

[As we start on eclipse it will ask for choose workspace. We can see the browse for folder dialog at that time] Thanks in advance.

Upvotes: 34

Views: 87214

Answers (4)

Vit Bernatik
Vit Bernatik

Reputation: 3802

Pre-chewed code:

JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new java.io.File(".")); // start at application current directory
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = fc.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
    File yourFolder = fc.getSelectedFile();
}

Upvotes: 24

adit
adit

Reputation: 281

JFileChooser j = new JFileChooser();
j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
Integer opt = j.showSaveDialog(this);

Upvotes: 28

Bibhaw
Bibhaw

Reputation: 497

Use JFIleChooser. e.g.

JFileChooser chooser = new JFileChooser("C:\example");

for details please go through:

http://leepoint.net/notes-java/GUI/containers/20dialogs/30filechooser.html

http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html

Upvotes: 1

Stathis Alexopoulos
Stathis Alexopoulos

Reputation: 1009

You can force JFileChooser to select only folders, if you add the following command.

        _fileChooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY);

in the snippet that Bibhaw posted.

Upvotes: 54

Related Questions