Browse for folder dialog
Asked Answered
C

4

34

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.

Curvature answered 24/1, 2011 at 6:45 Comment(2)
Is it possible with AWT (without customizing an own DirChooser) ?Pirbhai
Simply: JFileChooser + setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)Enthusiastic
D
54

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.

Denotative answered 24/1, 2011 at 7:53 Comment(0)
D
28
JFileChooser j = new JFileChooser();
j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
Integer opt = j.showSaveDialog(this);
Desperate answered 15/1, 2013 at 2:39 Comment(1)
"this" in ShowSaveDialog is your JFrame variableCrowns
D
24

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();
}
Dubitation answered 14/5, 2015 at 20:6 Comment(0)
M
1

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

Mairamaire answered 24/1, 2011 at 6:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.