This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
Need help with FileFilter
Posted by Thi on November 28, 2001 at 3:15 PM
Hello, I would like to restrict files shown in a file dialog. I've tried to do this by implementing the FileFilter interface, exactly shown in the Core Java book in Chapter 9 under File Dialogs. However, I'm getting an error stating that FileFilter must be an interface. Can someone look at my code and tell me what I have done wrong? Or, can someone show me another way to filter files? Here is my code, which creates a Frame that contains two buttons--one for an open dialog box and the other for a save dialog box: import java.io.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.filechooser.*; import javax.swing.filechooser.FileFilter; public class FileChooserDemo extends JFrame { static private final String newline = "\n"; //This is where the compiler says FileFilter must be an //interface class TxtFilter implements javax.swing.filechooser.FileFilter { public boolean accept(File f) { return f.getName().toLowerCase().endsWith(".txt") || f.isDirectory(); } public String getDescription() { return "Text file"; } } public FileChooserDemo() { super("FileChooserDemo");
//Create the log first, because the action listeners //need to refer to it. final JTextArea log = new JTextArea(5,20); log.setMargin(new Insets(5,5,5,5)); log.setEditable(false); JScrollPane logScrollPane = new JScrollPane(log); //Create a file chooser final JFileChooser fc = new JFileChooser(); fc.setCurrentDirectory(new File(".")); fc.setFileFilter(new TxtFilter()); //sets the filter to .txt files //Create the open button JButton openButton = new JButton("Open a File..."); openButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int returnVal = fc.showOpenDialog(FileChooserDemo.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); //this is where a real application would open the file. log.append("Opening: " + file.getName() + "." + newline); } else { log.append("Open command cancelled by user." + newline); } } }); //Create the save button JButton saveButton = new JButton("Save a File..."); saveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int returnVal = fc.showSaveDialog(FileChooserDemo.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); //this is where a real application would save the file. //export the range of events as a .cal file, which can be imported String filename = file.getName(); log.append("Saving: " + file.getName() + "." + newline); } else { log.append("Save command cancelled by user." + newline); } } }); //For layout purposes, put the buttons in a separate panel JPanel buttonPanel = new JPanel(); buttonPanel.add(openButton); buttonPanel.add(saveButton); //Explicitly set the focus sequence. openButton.setNextFocusableComponent(saveButton); saveButton.setNextFocusableComponent(openButton); //Add the buttons and the log to the frame Container contentPane = getContentPane(); contentPane.add(buttonPanel, BorderLayout.NORTH); contentPane.add(logScrollPane, BorderLayout.CENTER); } public static void main(String[] args) { JFrame frame = new FileChooserDemo(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.pack(); frame.setVisible(true); } }
Replies:
|