jfilechooser如何在DIRECTORIES_ONLY模式下隐藏文件夹名称中的完整路径 [英] jfilechooser how to hide full path in folder name in DIRECTORIES_ONLY mode

查看:307
本文介绍了jfilechooser如何在DIRECTORIES_ONLY模式下隐藏文件夹名称中的完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将jfilechooser设置为DIRECTORIES_ONLY模式。
对于当前的jfilechooser,我不满意的是在directorys_only模式下选择文件夹,该操作在文件夹名称中显示了完整的绝对路径。
是否有任何方法可以覆盖此行为,仅在文件情况下才显示文件夹名称?

I have a jfilechooser set to DIRECTORIES_ONLY mode. What I do not like about my current jfilechooser is selecting folders in the directories_only mode shows the full absolute path in the folder name. Is there any way to override this behavior to just show the folder name only like in the case of a file?

我的jfilechooser就是
为要创建的文件夹指定名称,以将内容保存到

(如果选择了文件夹),覆盖该文件夹

My jfilechooser is meant to specify a name for the folder that is about to be created to save contents to OR if a folder is selected, overwrite that folder

推荐答案

我不是用Java进行编码,而是以kawa(jvm方案)进行编码,所以我只能给出像Java一样的解决方案。

I am not coding in java but in kawa (jvm scheme) so I can only give the solution as java-like as I can.

基本上,我向我的JFileChooser
Override propertyChange方法添加了PropertyChangeListener来执行以下
,我听属性更改SELECTED_FILE_CHANGED_PROPERTY并将文件名显示手动设置为JFileChooser的一部分FileChooserUI。

Basically I added a PropertyChangeListener to my JFileChooser Override propertyChange method to do the following I listen to the property change SELECTED_FILE_CHANGED_PROPERTY and set the file name display manually to the FileChooserUI which is part of JFileChooser.

请注意,所有这些只是为了美观,选择的文件夹无论如何都不会改变。只是文件名显示不应该以这种方式显示完整路径,而只是您刚选择的文件夹名称。

Note that all these is just for aesthetics, the folder chosen is not in anyway changed. It is just that the filename display would not should the full path this way but only the name of the folder you just selected.

这是我尝试编写不带Java代码的尝试测试。当我有更多时间时,我将再次进行测试。

Here is my attempt to write java code without testing. I'll test this again when I have more time.

JFileChooser folder_chooser = new JFileChooser();

folder_chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);

PropertyChangeListener folder_name_changer = new PropertyChangeListener () {
    public void propertyChange(PropertyChangeEvent e) {
        String property_name = e.getPropertyName();
        JFileChooser chooser = e.getSource();
        if (property_name.equals(JFileChooserSELECTED_FILE_CHANGED_PROPERTY) {
            File selected_file = chooser.getSelectedFile();
            FileChooserUI chooser_ui = chooser.getUI();

            // BasicFileChooserUI is the subclass that implements a setFileName method
            if ( selected_file != null && (chooser_ui instanceof BasicFileChooserUI)) {
                chooser_ui.setFileName( selected_file.getFileName() );
            }
        } 
    }
};

folder_chooser.addPropertyChangeListener( folder_name_changer );

这篇关于jfilechooser如何在DIRECTORIES_ONLY模式下隐藏文件夹名称中的完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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