Browse for folder dialog

社会主义新天地 提交于 2019-12-02 20:15:35

Sounds like you'll want to use a JFileChooser. Oracle has a bunch of tutorials on basic Swing components and how to use them. Here's their tutorial page for the JFileChooser: link.

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.

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

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();
}
Bibhaw

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!