为 JFileChooser 添加选择文件有效验证功能

版权声明:转载时请务必保留以下作者信息和链接
作者:陈维(chenweionline@hotmail.com)作者的网站:http://www.chenwei.mobi

我们在做 GUI 编程的时候经常需要用到 JFileChooser 组件构造一个文件选择对话框来为用户提供打开文件、保存文件等操作。

通常的做法是调用 JFileChooser.showOpenDialog() 方法显示文件选择对话框并且选择一个文件后,点击【Approve】按钮(默认情况下标有 "Open" ),当对话框关闭后使用 JFileChooser.getSelectedFile() 方法得到选取的文件,然后再对被选取的文件有效性进行验证(例如,文件的文件名是否合法、选取的路径下是否已有同名文件存在等等),如果验证不通过,需要再次打开文件选择对话框进行选取。

显然,验证没有通过的情况下,文件选取对话框被反复的打开和关闭,影响用户体验。

我现在介绍一个方法,通过对 JFileChooser 进行一些改进,在点击了【Approve】按钮后,对话框关闭前对选取的文件进行有效性验证,如果验证没有通过,那么 JFileChooser 不会被关闭,而在其上会弹出一个警告对话框,关闭这个警告对话框后可以直接在 JFileChooser 进行下一次选择。

现在我们来实现这个特殊的文件选择对话框。

查看 JFileChooser 的 API,可以发现这样一个方法 public void approveSelection() 。这个方法会在用户确定选择操作时(例如单击【Approve】按钮)由 UI 调用,导致使用等于 APPROVE_SELECTION 的命令字符串激发一个操作事件。

那么,我们现在需要做的就是继承 JFileChooser 再覆写这个方法,将对选中文件的有效性验证写入这个方法中,只有当验证通过时才调用超类的 approveSelection() 完成文件选取,否则弹出警告对话框直接返回,继续选择新的文件。

下面的代码就是这个特殊文件选择对话框的实现

package mobi.chenwei.wing.swing;

import java.io.File;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileSystemView;
import mobi.chenwei.wing.swing.util.Utilities;

/***/ /**
*带文件有效验证的文件选择对话框。
*
*
@authorChenWei
*@websitewww.chenwei.mobi
*@emailchenweionline@hotmail.com
*/

public class JValidateFileChooser extends javax.swing.JFileChooser ... {

publicJValidateFileChooser()...{
super();
}


publicJValidateFileChooser(FilecurrentDirectory)...{
super(currentDirectory);
}


publicJValidateFileChooser(FilecurrentDirectory,FileSystemViewfsv)...{
super(currentDirectory,fsv);
}


publicJValidateFileChooser(FileSystemViewfsv)...{
super(fsv);
}


publicJValidateFileChooser(StringcurrentDirectoryPath)...{
super(currentDirectoryPath);
}


publicJValidateFileChooser(StringcurrentDirectoryPath,FileSystemViewfsv)...{
super(currentDirectoryPath,fsv);
}


/***//**
*超类的这个方法会在用户确定选择操作时由UI调用,
*现在覆写这个方法在其中进行有效性验证。
*/

@Override
publicvoidapproveSelection()...{
Filefile
=getSelectedFile();

//验证文件名是否合法,合法文件名不能包含'','/',':','*','?','"','<','>','|'等字符。
if(!validateFileName(file.getName()))...{
JOptionPane.showMessageDialog(getParent(),
"Invalidfilenameorpath.","WARNING",JOptionPane.WARNING_MESSAGE);
return;
}


//检查选中的文件是否存在。
if(!file.exists())...{
JOptionPane.showMessageDialog(getParent(),
"Cannotfindtherequestedfile.","WARNING",JOptionPane.WARNING_MESSAGE);
return;
}


super.approveSelection();
}


privatebooleanvalidateFileName(Stringname)...{
if(name.indexOf('\')!=-1||name.indexOf('/')!=-1||
name.indexOf(
':')!=-1||name.indexOf('*')!=-1||
name.indexOf(
'?')!=-1||name.indexOf('"')!=-1||
name.indexOf(
'<')!=-1||name.indexOf('>')!=-1||
name.indexOf(
'|')!=-1)...{

returnfalse;
}
else...{
returntrue;
}

}


publicstaticvoidmain(String[]args)...{
JValidateFileChooserchooser
=newJValidateFileChooser();
chooser.showOpenDialog(
null);
}

}

程序运行截图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值