java swiming实现文件夹选择

来源:互联网 发布:小数点的算法 编辑:程序博客网 时间:2024/06/16 17:23
int result = 0;
File file = null;
String path = null;
JFileChooser fileChooser = new JFileChooser(); JFileChooser 为用户选择文件提供了一种简单的机制。
FileSystemView fsv = FileSystemView.getFileSystemView(); //注意了,这里重要的一句 //文件
System.out.println(fsv.getHomeDirectory()); //得到桌面路径
fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
进入文件管理器的默认显示

fileChooser.setDialogTitle("请选择要上传的文件..."); //提示框
fileChooser.setApproveButtonText("确定"); //确定按钮
    这是尤为重要的。因为JFileChooser默认的是选择文件,而需要选目录。 
    故要将DIRECTORIES_ONLY装入模型 
另外,若选择文件,则无需此句 
*/ 
      fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); //设置只选择文件,或者目录. 与都可以
result = fileChooser.showOpenDialog(chatFrame); 打开文件对话框
if (JFileChooser.APPROVE_OPTION == result) {
path=fileChooser.getSelectedFile().getPath();
System.out.println("path: "+path);
}