I need to display the file name without the extension in JFileChooser(open mode). How?

你离开我真会死。 提交于 2019-12-31 04:23:29

问题


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

This is my FileView code:

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

I use this:

        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));

        }
    });

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


回答1:


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());

Edit

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.



来源:https://stackoverflow.com/questions/9189791/i-need-to-display-the-file-name-without-the-extension-in-jfilechooseropen-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!