问题
I have a method in my application called "Save as" which Saves the image of my application on computer my into a file. I used the JFileChooser to let the users choose their desired location for saving the file. The problem is unless user explicitly types in the file format, it saves the file with no extension. How can I have formats like jpg, png in the File Type drop down menu.
and, how can i get extension from the File Type drop menu for saving my image file.
ImageIO.write(image,extension,file);
回答1:
Finally, i solve my own problem :-
JFileChooser FC=new JFileChooser("C:/");
FC.addChoosableFileFilter(new jpgSaveFilter());
FC.addChoosableFileFilter(new jpegSaveFilter());
FC.addChoosableFileFilter(new PngSaveFilter());
FC.addChoosableFileFilter(new gifSaveFilter());
FC.addChoosableFileFilter(new BMPSaveFilter());
FC.addChoosableFileFilter(new wbmpSaveFilter());
int retrival=m_fileChooser_save.showSaveDialog(null);
if (retrival == m_fileChooser_save.APPROVE_OPTION)
{
String ext="";
String extension=m_fileChooser_save.getFileFilter().getDescription();
if(extension.equals("*.jpg,*.JPG"))
{
ext=".jpg";
}
if(extension.equals("*.png,*.PNG"))
{
ext=".png";
}
if(Extension.equals("*.gif,*.GIF"))
{
ext=".gif";
}
if(extension.equals("*.wbmp,*.WBMP"))
{
ext=".wbmp";
}
if(Extension.equals("*.jpeg,*.JPEG"))
{
EXT=".jpeg";
}
if(extension.equals("*.bmp,*.BMP"))
{
ext=".bmp";
}
Example Filter:
import java.io.*;
import java.io.File;
import java.util.*;
import javax.swing.filechooser.FileFilter;
class jpgSaveFilter extends FileFilter
{
public boolean accept(File f)
{
if (f.isDirectory())
{
return false;
}
String s = f.getName();
return s.endsWith(".jpg")||s.endsWith(".JPG");
}
public String getDescription()
{
return "*.jpg,*.JPG";
}
}
回答2:
Prepare the file chooser filters:
jFileChooser.addChoosableFileFilter(new FileNameExtensionFilter("File X (.xxx)", "xxx"));
jFileChooser.addChoosableFileFilter(new FileNameExtensionFilter("File Y (.yyy)", "yyy"));
jFileChooser.addChoosableFileFilter(new FileNameExtensionFilter("File Z (.zzz)", "zzz"));
// set default type
jFileChooser.setFileFilter(jFileChooser.getChoosableFileFilters()[0]);
// set default file
jFileChooser().setSelectedFile(defaultFile);
After approve validation
//Add extension to Selected file
File file = new File(jFileChooser().getSelectedFile().getCanonicalPath() + "." + ((FileNameExtensionFilter) jFileChooser().getFileFilter()).getExtensions()[0]);
Could be a good idea validate if the selected file come with extension.
回答3:
Use JFileChoose.SetFileFilter example: http://www.java2s.com/Code/JavaAPI/javax.swing/JFileChoosersetFileFilterFileFilterfilter.htm
回答4:
I think I got better solution. Will explain it with sample code fragments.
This is how I set file filter:jFileChooser.setFileFilter(new FileNameExtensionFilter(".txt", "txt"));
.
After this the main saving line:textArea1.write(new BufferedWriter(new FileWriter(jFileChooser.getSelectedFile().getAbsolutePath() + jFileChooser.getFileFilter().getDescription().replace("All Files", ""))));
.
Of course the most important is this fragment: jFileChooser.getSelectedFile().getAbsolutePath() + jFileChooser.getFileFilter().getDescription().replace("All Files", "")
.
The only thing I don't like is, that I could not find any method like 'getExtension' which means that You cannot have any nice description without unnecessary troubles with strings.
Ok, got it. You can do something like that:
jFileChooser.getFileFilter().toString().replaceFirst(".*extensions=\\[(.*)]]", ".$1").replaceFirst(".*AcceptAllFileFilter.*", "")
.
Unfortunately it's not so beautiful, but seems to work like a charm.
来源:https://stackoverflow.com/questions/2531037/how-to-save-file-using-jfilechooser