How to filter file type in FileDialog?
-
03-07-2021 - |
Вопрос
I am using FileDialog for saving and loading in a Java program.
How can I filter the dropdown list to specify the file type as "JPG" or "JPEG" etc. ?
I have tried the following code, but it seems to have no effect.
Are there any other ways of doing this ?
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);
}
Решение
I've always used JFileChooser instead of FileDialog. You can then add multiple ChoosableFileFilters for the various types your program will support this way:
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
}
Below is my implementation of a 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;
}
}
Другие советы
The answer is simple. You can use
saveFileDialog.setFile("*.jpg;*.jpeg");
No need to use setFilenameFilter
method. You can add as many file type as you which.
Solving of this problem can be found in huxhorn' s comment in Bug ID: 4031440 FileDialog doesn't call FilenameFilter.accept().
public void actionPerformed(ActionEvent e) {
FileDialog saveFileDialog = new FileDialog(new Frame(), "Save", FileDialog.SAVE);
saveFileDialog.setFile("*.jpg;*.jpeg");
saveFileDialog.setVisible(true);
}
It doesn't work for me either. Filename filters do not function in Sun's reference implementation for Microsoft Windows.
Instead, you could try the below code. You should also remove the setFilenameFilter
part:
saveFileDialog.setFile("*.jpg");
Starting with Java 8 you can a Lambda expression:
fileDialog.setFilenameFilter((File dir, String name) -> name.endsWith(".html"));