对JFileChooser操作的一点心得

前段时间因为工作原因,需要对JFileChooser中选中或手动输入的文件名进行一下限制,也就是对其中的JTextField的输出进行一下限制,首先面临的问题就是怎么从JFileChooser这个控件中提出JTextField这个子空间,在网上搜索了一下,发现这个方法比较好用,代码如下:
  1. public static Component getLabelForInChooser(JFileChooser chooser,
  2.             String key) {
  3.         java.util.Locale l = chooser.getLocale();
  4.         String s = UIManager.getString(key, l);
  5.         javax.swing.plaf.FileChooserUI ui = chooser.getUI();
  6.         int count = ui.getAccessibleChildrenCount(chooser);
  7.         for (int i = 0; i < count; i++) {
  8.             javax.accessibility.Accessible a = ui
  9.                     .getAccessibleChild(chooser, i);
  10.             JLabel label = findLabel((JComponent) a, s);
  11.             if (label != null) {
  12.                 return label.getLabelFor();
  13.             }
  14.         }
  15.         return null;
  16.     }
  17.     private static JLabel findLabel(JComponent comp, String s) {
  18.         JLabel label = null;
  19.         if (comp instanceof JLabel) {
  20.             if (((JLabel) comp).getText().equals(s)) {
  21.                 label = (JLabel) comp;
  22.             }
  23.         } else if (comp instanceof JComponent) {
  24.             Component[] comps = comp.getComponents();
  25.             for (int i = 0; i < comps.length; i++) {
  26.                 if (comps[i] instanceof JComponent) {
  27.                     label = findLabel((JComponent) comps[i], s);
  28.                     if (label != null) {
  29.                         break;
  30.                     }
  31.                 }
  32.             }
  33.         }
  34.         return label;
  35.     }
  36. public JTextField getJTextField(){
  37.         Component comp = getLabelForInChooser(this,
  38.                                 "FileChooser.fileNameLabelText");
  39.         if (comp instanceof JTextField) {
  40.             return ((JTextField) comp);
  41.         }
  42.         return null;
  43.     }
这段代码大部分是在网上找到的,基本没作修改。
提出JTextField 的工作完成之后,就进入了下一步限制
JTextField 的过程,因为我要显示的文件名必须以CP_开头,所以代码修改如下:

  1. protected void setJTextField(){
  2.         JTextField field = getJTextField();
  3.         field.setDocument(new specialDocment());
  4.         field.setText("CP_");
  5.     }
  6. protected class specialDocment extends PlainDocument {
  7.         public void remove(int offs, int len) {
  8.             
  9.             try {
  10.                 if (offs >= strTop.length()) {
  11.                     super.remove(offs, len);
  12.                 }
  13.             } catch (BadLocationException e) {
  14.                 e.printStackTrace();
  15.             }
  16.             
  17.         }
  18.         public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
  19.             
  20.             try {
  21.                 if(offs < strTop.length() && getLength() != 0) {
  22.                     if(offs == 0 && str.startsWith(strTop)){
  23.                         File file = getSelectedFile();
  24.                         if(null != file && file.exists()){
  25.                         super.remove(offs, getLength());
  26.                         super.insertString(offs, str, a);
  27.                         }
  28.                     }
  29.                 }
  30.                 else{
  31.                     super.insertString(offs, str, a);
  32.                 }
  33.             } catch (BadLocationException e) {
  34.             }
  35.         }
  36.     }
最后又重写了一下approveSelection(),对文件名进行了一下格式检查,简单的完成了任务。
在查询的过程中学习是一件挺不错的事,加油!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JFileChooser 是 Java Swing 中的一个类,它提供了一个对话框,允许用户浏览文件系统并选择文件或目录。通过使用 JFileChooser,可以让用户轻松地选择文件或目录,而不需要编写自己的文件浏览器界面。 JFileChooser 提供了各种选项,可以设置文件过滤器、文件选择模式、默认文件名等。可以将 JFileChooser 放置在 JFrame 窗口中,也可以在对话框中使用它。当用户选择文件或目录后,JFileChooser 将返回一个 File 对象,可以使用它来打开、读取或保存文件。 以下是一些 JFileChooser 的用法示例: 1. 打开文件选择对话框并选择文件: ``` JFileChooser chooser = new JFileChooser(); int result = chooser.showOpenDialog(null); if (result == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); // 处理选中的文件 } ``` 2. 打开文件选择对话框并选择目录: ``` JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int result = chooser.showOpenDialog(null); if (result == JFileChooser.APPROVE_OPTION) { File selectedDirectory = chooser.getSelectedFile(); // 处理选中的目录 } ``` 3. 打开文件选择对话框并设置默认文件名: ``` JFileChooser chooser = new JFileChooser(); chooser.setSelectedFile(new File("example.txt")); int result = chooser.showSaveDialog(null); if (result == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); // 处理选中的文件 } ``` 需要注意的是,JFileChooser 可能会弹出一个安全警告框,提示用户是否允许访问文件系统。如果用户选择拒绝,JFileChooser无法从文件系统中读取文件或保存文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值