本文介绍了需要用的FileDialog在Java中的文件类型过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JDialog一个按钮/文本字段供用户选择一个文件。这里的code:

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

我的问题是,而不是看到的所有文件过滤器,我想提供一个自定义过滤器,例如为Word文档或东西。我安装使用setFilenameFilter(),但它似乎没有工作,一个自定义的FilenameFilter。我也注意到,它说,在文档中的自定义过滤器不工作在Windows(运行在Windows XP / Vista / 7的)。这里是我实施过滤器:

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

我是不是做错了什么吗?另外,我想说明出现在框中,如Microsoft Word中格式(* .doc * .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.

任何及所有帮助AP preciated。

Any and all help is appreciated.

推荐答案

AWT是不是真的编写Java GUI的preferred方式应用程式,这些天。太阳也大多放弃了。两种最流行的选项是Swing和。因此,我认为他们并没有真正开发的API非常广泛添加现代功能。 (错了,回答你的问题:不,你似乎没有能够做到与AWT)

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)

摇摆的优势在于它是真正的一次编写,随处运行,它可以看起来完全一样无处不在。有外观和放大器;认为试图使摆动一下本地的,有些是比别人做得更好键(Mac并不可怕,Windows是好,GTK不是)。不过,如果你想要一个应用程序,真正的外观和行为EXACTLY到处都一样,摇摆会让你做到这一点。再加上它运行外的开箱,没有任何额外的库。性能不是很大。

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的会让你做你想做的。创建的子类FileFilter并调用 setFileFilter 的JFileChooser

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

SWT取写一次随处运行的另一个极端。你还有你写违心codeBase的,但它实际上使用本机小部件每个平台上,因此通常看起来像一个本机应用程序(不完美随处可见,但仍IM pressive)。它的快速和以我的经验pretty可靠。月食(和其他高知名度的软件)使用SWT因此它在pretty大量使用。但它确实需要特定于平台的JAR文件和DLL。

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.

这篇关于需要用的FileDialog在Java中的文件类型过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:18