最后我找到了一个解决方案:使用派生自的自定义文件选择器 UIBasicFileChooserUI
将通过以下方式解决我的问题:我已经getApproveSelectionAction()
用我的自定义操作覆盖了该方法:
protected class CustomApproveSelectionAction extends BasicFileChooserUI.ApproveSelectionAction {
@Override
public void actionPerformed(ActionEvent e) {
String filename = getFileName();
// using a custom pattern to accept valid charachters only:
Matcher matcher = pattern.matcher(filename);
if (matcher.matches()) {
// this is the good case, just let the super implementation do what have to do.
super.actionPerformed(e);
} else {
// this is the bad case, we must warn the user and don't let the super implementation take effect.
// display an error message similar like notepad does it.
}
}
}
如果文件名没问题,那么我允许超级实现执行的操作,否则我将显示一条消息。