我需要一个 JFileChooser,它在保存模式下的作用类似于 notepad.exe
或 mspaint.exe
。您可能知道,当您在“文件名”字段中键入通配符(* 或 ?)字符时,文件 View 将仅显示与用户输入匹配的文件。这没问题,但我的问题是“文件类型”组合框:
在JFileChooser
中:当用户在文件名字段中输入通配符时,文件类型组合框也会更新。
看截图here !
但是,如果您使用 notepad.exe
尝试此操作,您会发现文件名字段仍保持以前的状态,因此搜索模式不会更新。
查看记事本的截图here !
所以我的问题是:有人知道如何实现文件类型组合框不应由输入的过滤器更新的解决方案吗?
我需要一个跨平台解决方案,因此它应该可以在 XP 和 Linux 上运行。
提前致谢!
抱歉提供了这些链接,但我不允许直接附加它!
最佳答案
终于找到解决办法了:
使用从 BasicFileChooserUI
派生的自定义文件选择器 UI 将通过以下方式解决我的问题:我已使用自定义操作覆盖 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.
}
}
}
如果文件名正确,那么我允许 super 实现执行的操作,否则我将显示一条消息。
关于java - 是否可以不在 JFileChooser 的文件类型组合框中添加键入的通配符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12392491/