我需要在JFileChooser(打开模式)中显示没有扩展名的文件名。怎么样? [英] I need to display the file name without the extension in JFileChooser(open mode). How?

查看:280
本文介绍了我需要在JFileChooser(打开模式)中显示没有扩展名的文件名。怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开放模式下使用'JFileChooser'。我需要显示没有扩展名的文件名字段。
怎么样?
我知道FileView。它删除文件系统文件中的扩展名,但它会在文件名字段中的选定文件中保留扩展名
说明

I use 'JFileChooser' in open mode. I need to display the 'file name' field without the extension. How?? I know the FileView. It remove extensions in file system's files, but it leaves the expansion in selected file in the field 'File name' explanation

这是我的FileView代码:

This is my FileView code:

public class JQSFileView extends FileView{
@Override
    public String getName(File file){
        return FilenameUtils.removeExtension(file.getName());
    }
}

我用这个:

        fc.addPropertyChangeListener(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY, new PropertyChangeListener()
    {
        public void propertyChange(PropertyChangeEvent evt)
        {

            File selectedFile = fc.getSelectedFile();
            String path = selectedFile.getPath();
            path=FilenameUtils.removeExtension(path);
            fc.setSelectedFile(new File(path));

        }
    });

但是JFileChooser返回的文件名不正确,当我选择一些文件时,光标总是跳起来

But JFileChooser return not right file name, and cursor always jump to up when I select some file

推荐答案

根本原因是ui不使用视图名称作为名称字段中的文本。哪个可能或可能是一个好主意,不知道。如果你真的想要,你可以在JFileChooser的子类或PropertyChangeListener中手动完成,这里是一个覆盖:

The underlying reason is that the ui doesn't use the view's name as text in the name field. Which may or may but be a good idea, don't know. If you really want that, you can do so manually, either in a subclass of JFileChooser or in a PropertyChangeListener, here's an override:

    fc = new JFileChooser() {

        @Override
        public void setSelectedFile(File file) {
            super.setSelectedFile(file);
            ((BasicFileChooserUI) getUI()).setFileName(getName(file));
        }

    };
    fc.setFileView(new MyView());

修改

outch ...没想到ui的错误行为:(问题是所有的动作都是根据textField的内容重新创建一个文件对象(而不是使用选择器的selectedFile属性) )现在,如果该字符串已修剪扩展名,则找不到该文件。唯一的出路是替换可能无效的操作......

outch ... hadn't expected so much mis-behaviour of the ui :-( Problem is that all the actions re-create a file object based on the content of the textField (instead of using the selectedFile property of the chooser) Now if that string has the extension trimmed, the file isn't found. The only way out would be to replace the actions ... which might not work.

总之,这个答案毫无用处,抱歉。

In summary, this answer is useless, sorry.

这篇关于我需要在JFileChooser(打开模式)中显示没有扩展名的文件名。怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆