... Next chapter is files; finally will have some data to work with. Now, if the weather would just get warm for one more bike ride before the snow!Well, the weather did get warm and I got in two bike rides, the file chapter was a disappointment (just a whitewash of DataOutputStream), so I decided to try DataInputStream which was also easy, BUT ...
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
Expand|Select|Wrap|Line Numbers
- import java.io.*;
- import javax.swing.*;
- class DatFilter extends javax.swing.filechooser.FileFilter
- {
- public boolean accept(File f)
- {
- return f.getName().endsWith(".dat");
- }
- public String getDescription ()
- {
- return "Data files (*.dat)";
- }
- }
- public class TestChooser
- {
- public static void main(String[] args)
- {
- JFileChooser fc = new JFileChooser();
- fc.setDialogTitle ("Open Payment File");
- fc.setFileSelectionMode ( JFileChooser.FILES_ONLY);
- fc.setCurrentDirectory (new File ("."));
- fc.setFileFilter(new DatFilter());
- int result = fc.showOpenDialog (null);
- if (result == JFileChooser.CANCEL_OPTION)
- System.exit(0);
- File fFile = fc.getSelectedFile();
- JOptionPane.showMessageDialog(null, fFile.getName());
- }
- }