Originally posted by SammyB from a previous thread
I wanted to add a GetOpenFileName dialog box (the book just hardcodes the filename), but I am mildly flummoxed by the FileFilter. The test program below works, but there is only one choice, Data Files. How can I have two choices in the Files of Type drop-down, say Text files (*.txt) and Data files (*.dat). Also, just a nit: in the DOS world, All files is usually the last choice, not the first. Can I put All files at the bottom of the list? TIA --Sam
[code=java]
import java.io.*;
import javax.swing.*;
class DatFilter extends javax.swing.fil echooser.FileFi lter
{
public boolean accept(File f)
{
return f.getName().end sWith(".dat");
}
public String getDescription ()
{
return "Data files (*.dat)";
}
}
public class TestChooser
{
public static void main(String[] args)
{
JFileChooser fc = new JFileChooser();
fc.setDialogTit le ("Open Payment File");
fc.setFileSelec tionMode ( JFileChooser.FI LES_ONLY);
fc.setCurrentDi rectory (new File ("."));
fc.setFileFilte r(new DatFilter());
int result = fc.showOpenDial og (null);
if (result == JFileChooser.CA NCEL_OPTION)
System.exit(0);
File fFile = fc.getSelectedF ile();
JOptionPane.sho wMessageDialog( null, fFile.getName() );
}
}
[/code]
Comment