import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
...
Preferences userPrefs = Preferences.userRoot().node( "/com/mindprod/mypackage" );
File suggestedFile = new File ( userPrefs.get( "SAVEDIR", "C:" ), saveName );
JFileChooser fc = new JFileChooser();
fc.setSelectedFile( suggestedFile );
fc.addChoosableFileFilter( new JpgFileFilter() );
int result = fc.showSaveDialog( this );
switch ( result )
{
case JFileChooser.APPROVE_OPTION:
File file = fc.getSelectedFile();
...
...
userPrefs.put( "SAVEDIR", fc.getCurrentDirectory().getAbsolutePath() );
break;
case JFileChooser.CANCEL_OPTION:
case JFileChooser.ERROR_OPTION:
break;
default:
}
----------------------------------------------------------------------------------------
import java.io.File;
import javax.swing.filechooser.FileFilter;
/**
* Creates a file filter that accepts only *.jpg files.
*
* @author Roedy Green
* @version 1.0
* @since 2004-05-06
*/
public class JpgFileFilter extends javax.swing.filechooser.FileFilter
{
/**
* Creates a file filter that accepts only *.jpg files.
*
*/
public JpgFileFilter()
{
}
/**
* Return true if this file should be shown in the directory pane,
* false if it shouldn't.
*
* accepts directories and.jpg files.
*
* @see FileFilter#accepts
*/
public boolean accept( File f )
{
if ( f != null )
{
if ( f.isDirectory() )
{
return true;
}
String name = f.getName().toLowerCase();
return name.endsWith( ".jpg" );
}
else
{
return false;
}
}
/**
* Returns the human readable description of this filter.
* @see setDescription
* @see setExtensionListInDescription
* @see isExtensionListInDescription
* @see FileFilter#getDescription
*/
public String getDescription()
{
return "*.jpg image files";
}
}