Java 文件选择器中的自动完成

发布于 2024-10-17 10:43:29 字数 226 浏览 14 评论 0原文

我正在寻找一种在 Java 文件选择器对话框中添加自动完成功能的方法。似乎 Java Swing JFileChooser 不支持这一点。我发现的唯一替代方案是 gtk FileChooser 我很想知道是否存在其他替代方案。

I'm looking for a way to add auto complete functionality in Java a File Chooser Dialog. Seems Java Swing JFileChooser doesn't support that. The only alternative I found is gtk FileChooser I would love to whether any other alternatives exist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

衣神在巴黎 2024-10-24 10:43:29

一种替代方法是使用 SWTFileDialog - 这使用本机操作系统文件对话框(例如 Linux 上的 GTK),因此您可以从用户的操作系统中获得与用户习惯的完全相同的行为。这包括 Linux/GTK 和 Mac 上的自动完成功能(我不知道 Windows 是否也提供了这个功能?)

我完全不确定是否值得为此目的而包含 SWT,但我想提一下这个替代方案。

One alternative would be to use SWT's FileDialog - this uses the native operating system file dialog (e.g. GTK on Linux), so you get the exact same behaviour which the users are used to from their OS. This includes autocomplete on Linux/GTK and Mac (I don't know, if Windows provides that, too?)

I'm not sure at all, if it's worth to include SWT just for that purpose, but I wanted to mention this alternative.

洛阳烟雨空心柳 2024-10-24 10:43:29

如果一个人添加了自动功能,那么他/她必须访问文本字段,但这以正常方式是不可能的。

存在一种黑客方法,它搜索所有组件,然后找到文本字段的实例并返回它。

这是代码..

private  JTextField dis(JFileChooser jf) {
    boolean hide =false;
    LinkedList<Component> queue = new LinkedList<Component>();
    queue.add(jf);
    JTextField jtf=new JTextField();
    while(queue.size() != 0) {
        Component[] c = ((Container) queue.removeFirst()).getComponents();   
        for(int i = 0; i < c.length; i++) {                        
            queue.add(c[i]);    

            if(c[i] instanceof JTextField) {
                jtf = (JTextField) c[i];
                jtf.setVisible(true);
                jtf.setEnabled(true);
                jtf.setText(" hello ");
                return jtf;
            }


    }           
}


return jtf;

}

这是扩展应用自动完成的 JFileChooser 的类的链接
http://commondatastorage.googleapis.com/holyvincent/FileChooser/VinFC.java

如果扩展类中存在错误,请报告。
谢谢

if one has add auto functionality then he/she has to access the text field but that is not possible in normal ways..

A kind of hack was is there which searches for all the components and then finds the instance of textfield and returns it.

here is the code..

private  JTextField dis(JFileChooser jf) {
    boolean hide =false;
    LinkedList<Component> queue = new LinkedList<Component>();
    queue.add(jf);
    JTextField jtf=new JTextField();
    while(queue.size() != 0) {
        Component[] c = ((Container) queue.removeFirst()).getComponents();   
        for(int i = 0; i < c.length; i++) {                        
            queue.add(c[i]);    

            if(c[i] instanceof JTextField) {
                jtf = (JTextField) c[i];
                jtf.setVisible(true);
                jtf.setEnabled(true);
                jtf.setText(" hello ");
                return jtf;
            }


    }           
}


return jtf;

}

and here is the link for class that extends JFileChooser that applies autocomplete
http://commondatastorage.googleapis.com/holyvincent/FileChooser/VinFC.java

Please report if there are bugs in the extended class.
Thanks

把回忆走一遍 2024-10-24 10:43:29

如何实现您自己的 FileChooser,您可以将 DocumentListener 添加到 TextField,该 TextField 在文件选择器中保存您的文件路径 + 文件名。不确定您是否可以使用 JFileChooser 执行上述操作。如果您想让文件选择器的行为与默认的 JFileChooser 以及添加的自动完成功能相匹配,那么这将需要一些代码。

How about implementing your own FileChooser where you will be able to add a DocumentListener to the TextField which holds your filepath + filename in the file chooser. Not sure if you can do the above with a JFileChooser. It will be SOME amount of code though if you want to get the behaviour of your file chooser to match the default JFileChooser along with the added autocomplete feature.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文