需要 Java 中带有文件类型过滤器的 FileDialog

发布于 2024-08-01 14:28:03 字数 882 浏览 5 评论 0原文

我有一个带有按钮/文本字段的 JDialog,供用户选择文件。 代码如下:

FileDialog chooser = new FileDialog(this, "Save As", FileDialog.SAVE );
String startDir = saveAsField.getText().substring( 0, saveAsField.getText().lastIndexOf('\\') );
chooser.setDirectory(startDir);
chooser.setVisible(true);
String fileName = chooser.getFile();

我的问题是,我不想看到“所有文件”过滤器,而是想提供一个自定义过滤器,例如用于 Word 文档或其他内容。 我使用 setFilenameFilter() 设置了自定义 FilenameFilter,但它似乎不起作用。 我确实注意到文档中说自定义过滤器在 Windows 中不起作用(这在 Windows XP/Vista/7 中运行)。 这是我的过滤器实现:

chooser.setFilenameFilter( new geFilter() );
public class geFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return name.endsWith( ".doc" ) || name.endsWith( ".docx" );
    }
}

我在这里做错了什么吗? 另外,我希望在框中显示说明,例如“Microsoft Word (*.doc *.docx)”,但我不知道该怎么做。

感谢任何和所有的帮助。

I have a JDialog with a button/textfield for the user to select a file. Here's the code:

FileDialog chooser = new FileDialog(this, "Save As", FileDialog.SAVE );
String startDir = saveAsField.getText().substring( 0, saveAsField.getText().lastIndexOf('\\') );
chooser.setDirectory(startDir);
chooser.setVisible(true);
String fileName = chooser.getFile();

My problem is that instead of seeing an All Files filter, I want to provide a custom filter, e.g. for Word docs or something. I setup a custom FilenameFilter using setFilenameFilter(), but it didn't seem to work. I did notice that it says in the docs that the custom filter doesn't work in Windows (this runs in Windows XP/Vista/7). Here was my implementation of the filter:

chooser.setFilenameFilter( new geFilter() );
public class geFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return name.endsWith( ".doc" ) || name.endsWith( ".docx" );
    }
}

Am I doing something wrong here? Also, I want a description to appear in the box, like "Microsoft Word (*.doc *.docx)" but I'm not sure how to do that.

Any and all help is appreciated.

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

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

发布评论

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

评论(6

世界等同你 2024-08-08 14:28:03

如今,AWT 实际上并不是编写 Java GUI 应用程序的首选方式。 Sun似乎已经基本上放弃了它。 两个最流行的选项是 Swing 和 SWT。 所以我认为他们并没有真正广泛地开发 API 来添加现代功能。 (呃,回答你的问题:不,你似乎无法使用 AWT 做到这一点)

Swing 的优点是它真正是一次编写,随处运行,并且在任何地方看起来都完全相同。 有Look & 感觉尝试让 Swing 看起来原生,有些比其他更好(Mac 还不错,Windows 还可以,GTK 则不然)。 不过,如果您想要一个在任何地方看起来和行为都完全相同的应用程序,Swing 可以让您做到这一点。 另外,它开箱即用,无需任何额外的库。 性能不太好。

Swing 的 JFileChooser 将让您执行以下操作你要。 创建 FileFilter 的子类并在 JFileChooser 上调用 setFileFilter

SWT 将“一次写入,随处运行”推向了相反的极端。 您仍然有一个可编写的代码库,但它实际上使用每个平台上的本机小部件,因此它通常看起来像一个本机应用程序(并非在所有地方都很完美,但仍然令人印象深刻)。 根据我的经验,它很快而且相当可靠。 Eclipse(和其他知名软件)使用 SWT,因此它的使用量相当大。 但它确实需要特定于平台的 JAR 和 DLL。

AWT isn't really the preferred way of writing Java GUI apps these days. Sun seems to have mostly abandoned it. The two most popular options are Swing and SWT. So I think they didn't really develop the APIs very extensively to add modern features. (err, to answer your question: No you don't appear to be able to do that with AWT)

Swing has the advantage that it is truly write-once-run-anywhere and it can look exactly the same everywhere. There are Look & Feels that try to make Swing look native, some are better than others (Mac isn't terrible, Windows is okay, GTK isn't). Still, if you want an app that really looks and acts EXACTLY the same everywhere, Swing will let you do that. Plus it runs out-of-the-box without any extra libraries. Performance isn't great.

Swing's JFileChooser will let you do what you want. Create a subclass of FileFilter and call setFileFilter on the JFileChooser.

SWT takes the write-once-run-anywhere to the opposite extreme. You still have one codebase that you write against, but it actually uses the native widgets on each platform so it generally looks like a native app (not perfect everywhere, but still impressive). It's fast and pretty reliable in my experience. Eclipse (and other high profile software) uses SWT so it's in pretty heavy use. But it does require platform-specific JARs and DLLs.

半寸时光 2024-08-08 14:28:03

既然您使用的是 JDialog,那是一个 swing 类,为什么不使用 JFileChooser

 JFileChooser fc = new JFileChooser("C:\\");
 fc.setFileFilter(new FileNameExtensionFilter("Microsoft Word (*.doc, *.docx)", "doc", "docx"));

FileNameExtensionFilter 是一个很好的 Java 6 类这正是你想要的。

since you are using JDialog, that is a swing class why not using JFileChooser?

 JFileChooser fc = new JFileChooser("C:\\");
 fc.setFileFilter(new FileNameExtensionFilter("Microsoft Word (*.doc, *.docx)", "doc", "docx"));

FileNameExtensionFilter is a nice Java 6 class that does exactly what you want.

睫毛上残留的泪 2024-08-08 14:28:03

您可以使用 JNI 调用本机 Windows Filedialog (CFileDialog)。 可以轻松地为CFileDialog 设置过滤器。

几个月前我为 CFileDialog 编写了一个简单的包装类,如果您有兴趣,可以从

Google 代码上的 Xfiledialog 项目

You can call the native Windows Filedialog (CFileDialog) with JNI. Filters can be set for CFileDialog easily.

I wrote a simple wrapper class for CFileDialog several months ago, If you are interested, you can get the source and binary from

Xfiledialog project on google code

甜`诱少女 2024-08-08 14:28:03

只需使用 FileDialog 实例 fdsetFilenameFilter 方法即可:

            fd.setFilenameFilter(new FilenameFilter()
                            {
                                @Override
                                public boolean accept(File file, String s)
                                {
                                    // enter code to return TRUE or FALSE here
                                    return s.contains(".txt");
                                }
                            });

Just use setFilenameFilter method of the FileDialog instance fd:

            fd.setFilenameFilter(new FilenameFilter()
                            {
                                @Override
                                public boolean accept(File file, String s)
                                {
                                    // enter code to return TRUE or FALSE here
                                    return s.contains(".txt");
                                }
                            });
强者自强 2024-08-08 14:28:03

如果您曾经使用过 JavaFX 2FileChooser 类将完全满足您的需要,而不会出现任何 JFileChooser/FileDialog 问题。 您还可以在 Swing 应用程序中嵌入 JavaFX 2 组件,但您需要 JavaFX 运行时< /a>.

例子:

    FileChooser fc = new FileChooser();
    FileChooser.ExtensionFilter filter;
    filter = new FileChooser.ExtensionFilter("Text files (*.txt)", "*.txt");
    fc.getExtensionFilters().add(filter);
    File f = fc.showOpenDialog(primaryStage);
    System.out.println(f);

If you ever use JavaFX 2, the FileChooser class will do exactly what you need without any of JFileChooser/FileDialog problems. You can also embed JavaFX 2 components inside Swing applications, but you need JavaFX runtime.

Example:

    FileChooser fc = new FileChooser();
    FileChooser.ExtensionFilter filter;
    filter = new FileChooser.ExtensionFilter("Text files (*.txt)", "*.txt");
    fc.getExtensionFilters().add(filter);
    File f = fc.showOpenDialog(primaryStage);
    System.out.println(f);
染年凉城似染瑾 2024-08-08 14:28:03

我也在努力这样做。 我想使用 FileDialog 而不是 JFileChooser。

我在这里找到了答案: http://www.rgagnon.com/javadetails/java-0247 .html

他说“在Win平台上,setFilenameFilter方法不起作用。我们必须使用setFile方法来设置过滤器。”

在指定的链接中有源代码。

我测试过并且有效:

FileDialog fd = new FileDialog((Frame) null, "Save File", FileDialog.SAVE);
fd.setFile("*.txt");
fd.setVisible(true);

String file = fd.getFile();
System.out.println(file);
System.exit(0);

I am also trying to do that. I want to use FileDialog instead of JFileChooser.

I found the answer here: http://www.rgagnon.com/javadetails/java-0247.html

He says that "on the Win platform, the setFilenameFilter method don't work. We must use the setFile method instead to set a filter."

There is source code at the specified link.

I tested and it works:

FileDialog fd = new FileDialog((Frame) null, "Save File", FileDialog.SAVE);
fd.setFile("*.txt");
fd.setVisible(true);

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