JFileChooser: addChoosableFileFilter(FileFilter filter) : JFileChooser « javax.swing « Java by API






JFileChooser: addChoosableFileFilter(FileFilter filter)

  
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;

public class MainClass extends JFrame implements ActionListener {
  MainClass(String title) {
    super(title);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p = new JPanel();

    JButton jb = new JButton("Open ...");
    jb.addActionListener(this);
    p.add(jb);

    jb = new JButton("Save ...");
    jb.addActionListener(this);
    p.add(jb);

    getContentPane().add(p);

    setSize(200, 65);
    setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {
    int retVal;

    JFileChooser fc = new JFileChooser();

    if (e.getActionCommand().equals("Open ...")) {
      fc.addChoosableFileFilter(new TextFilter());
      retVal = fc.showOpenDialog(this);
    } else
      retVal = fc.showSaveDialog(this);

    if (retVal == JFileChooser.APPROVE_OPTION)
      System.out.println(fc.getSelectedFile().getName());
  }

  public static void main(String[] args) {
    new MainClass("FileChooser Demo");
  }
}

class TextFilter extends FileFilter {

  public boolean accept(File f) {
    if (f.isDirectory())
      return true;
    String s = f.getName();
    int i = s.lastIndexOf('.');

    if (i > 0 && i < s.length() - 1)
      if (s.substring(i + 1).toLowerCase().equals("txt"))
        return true;

    return false;
  }

  public String getDescription() {
    return "Accepts txt files only.";
  }
}

           
         
    
  








Related examples in the same category

1.JFileChooser.APPROVE_OPTION
2.JFileChooser.CANCEL_OPTION
3.JFileChooser.DIRECTORIES_ONLY
4.JFileChooser.DIRECTORY_CHANGED_PROPERTY
5.JFileChooser.ERROR_OPTION
6.JFileChooser.SELECTED_FILE_CHANGED_PROPERTY
7.JFileChooser.SELECTED_FILES_CHANGED_PROPERTY
8.JFileChooser: addActionListener(ActionListener l)
9.JFileChooser: addPropertyChangeListener(PropertyChangeListener listener)
10.JFileChooser: createDialog(Component parent)
11.JFileChooser: getCurrentDirectory()
12.JFileChooser: getFileSystemView()
13.JFileChooser: getSelectedFile()
14.JFileChooser: getTypeDescription(File f)
15.JFileChooser: isFileHidingEnabled()
16.JFileChooser: setAccessory(JComponent newAccessory)
17.JFileChooser: setApproveButtonMnemonic(char mnemonic)
18.JFileChooser: setApproveButtonText(String approveButtonText)
19.JFileChooser: setApproveButtonToolTipText(String toolTipText)
20.JFileChooser: setControlButtonsAreShown(boolean b)
21.JFileChooser: setCurrentDirectory(File dir)
22.JFileChooser: setFileFilter(FileFilter filter)
23.JFileChooser: setFileHidingEnabled(boolean b)
24.JFileChooser: setFileSelectionMode(int mode)
25.JFileChooser: setFileView(FileView fileView)
26.JFileChooser: setMultiSelectionEnabled(boolean b)
27.JFileChooser: setSelectedFile(File file)
28.JFileChooser: showOpenDialog(Component parent)