如何在FileDialog中过滤文件类型?

时间:2012-09-24 03:12:46

标签: java filedialog

我正在使用FileDialog在Java程序中保存和加载。

如何过滤下拉列表以将文件类型指定为“JPG”或“JPEG”等?

我尝试过以下代码,但似乎没有效果。

还有其他方法吗?

public void actionPerformed(ActionEvent e) {
            FileDialog saveFileDialog = new FileDialog(new Frame(), "Save", FileDialog.SAVE);

            saveFileDialog.setFilenameFilter(new FilenameFilter(){
                @Override
                public boolean accept(File dir, String name) {
                    return name.endsWith(".jpg") || name.endsWith(".jpeg");
                }
            });
            saveFileDialog.setFile("Untitled.jpg");
            saveFileDialog.setVisible(true);
        }

4 个答案:

答案 0 :(得分:8)

答案很简单。你可以使用

 saveFileDialog.setFile("*.jpg;*.jpeg");

无需使用setFilenameFilter方法。您可以添加与您一样多的文件类型。

可以在Bug ID: 4031440 FileDialog doesn't call FilenameFilter.accept() huxhorn 评论中找到解决此问题的方法。

public void actionPerformed(ActionEvent e) {
            FileDialog saveFileDialog = new FileDialog(new Frame(), "Save", FileDialog.SAVE);
            saveFileDialog.setFile("*.jpg;*.jpeg");
            saveFileDialog.setVisible(true);
        }

答案 1 :(得分:2)

它对我也不起作用。 Filename filters do not function in Sun's reference implementation for Microsoft Windows.

相反,您可以尝试以下代码。您还应该删除setFilenameFilter部分:

saveFileDialog.setFile("*.jpg");

答案 2 :(得分:1)

我一直使用JFileChooser而不是FileDialog。然后,您可以为程序支持的各种类型添加多个ChoosableFileFilters:

File myFilename;
chooser = new JFileChooser();
chooser.addChoosableFileFilter(new OpenFileFilter("jpeg","Photo in JPEG format") );
chooser.addChoosableFileFilter(new OpenFileFilter("jpg","Photo in JPEG format") );
chooser.addChoosableFileFilter(new OpenFileFilter("png","PNG image") );
chooser.addChoosableFileFilter(new OpenFileFilter("svg","Scalable Vector Graphic") );
int returnVal = chooser.showSaveDialog(mainWindow);
if (returnVal == JFileChooser.APPROVE_OPTION) {
     myFilename = chooser.getSelectedFile();
     //do something with the file
}

下面是我对FileFilter的实现。

/**
 * This class defines which file types are displayed (by default) by the JFileChooser and what file
 * types appear in the drop down menu in the file dialog.
 * You could add more than one file type to the open file dialog by creating multiple instances of this 
 * class and then repeatedly calling addFileFilter.
 * @author LaSpina
 */

import java.io.File;
import javax.swing.filechooser.*;

public class OpenFileFilter extends FileFilter {

    String description = "";
    String fileExt = "";

    public OpenFileFilter(String extension) {
        fileExt = extension;
    }

    public OpenFileFilter(String extension, String typeDescription) {
        fileExt = extension;
        this.description = typeDescription;
    }

    @Override
    public boolean accept(File f) {
        if (f.isDirectory())
            return true;
        return (f.getName().toLowerCase().endsWith(fileExt));
    }

    @Override
    public String getDescription() {
        return description;
    }
}

答案 3 :(得分:-1)

从Java 8开始,您可以使用Lambda表达式:

fileDialog.setFilenameFilter((File dir, String name) -> name.endsWith(".html"));