Example usage for javax.swing.filechooser FileFilter FileFilter

List of usage examples for javax.swing.filechooser FileFilter FileFilter

Introduction

In this page you can find the example usage for javax.swing.filechooser FileFilter FileFilter.

Prototype

FileFilter

Source Link

Usage

From source file:Main.java

/**
 * Displays a {@link JFileChooser} to select a file.
 *
 * @param title The title of the dialog.
 * @param startDirectory The directory where the dialog is initialed opened.
 * @param fileExtension File extension to filter each content of the opened
 * directories. Example ".xml".//from   www.  jav a2 s. c o m
 * @param startFile The preselected file where the dialog is initialed opened.
 * @return The {@link File} object selected, returns null if no file was selected.
 */
public static File chooseFile(String title, String startDirectory, final String fileExtension,
        String startFile) {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setDialogTitle(title);

    if (fileExtension != null && !fileExtension.trim().equals("")) {
        FileFilter filter = new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                return pathname.isDirectory() || pathname.getName().endsWith(fileExtension);
            }

            @Override
            public String getDescription() {
                return "(" + fileExtension + ")";
            }
        };

        chooser.setFileFilter(filter);
    }

    if (startDirectory != null && !startDirectory.trim().equals("")) {
        chooser.setCurrentDirectory(new File(startDirectory));
    }

    if (startFile != null && !startFile.trim().equals("")) {
        chooser.setSelectedFile(new File(startFile));
    }

    int status = chooser.showOpenDialog(null);

    if (status == JFileChooser.APPROVE_OPTION) {
        return chooser.getSelectedFile();
    }

    return null;
}

From source file:Main.java

public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    if (source == openItem) {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("."));
        chooser.setFileFilter(new FileFilter() {
            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".zip") || f.isDirectory();
            }/*from   w w w .java2 s .  c  o  m*/

            public String getDescription() {
                return "ZIP Files";
            }
        });
        int r = chooser.showOpenDialog(this);
        if (r == JFileChooser.APPROVE_OPTION) {
            String zipname = chooser.getSelectedFile().getPath();
            System.out.println(zipname);
        }
    } else if (source == exitItem)
        System.exit(0);
}

From source file:com.mgmtp.perfload.loadprofiles.ui.util.SwingUtils.java

public static JFileChooser createFileChooser(final File dir, final String description, final String extension) {
    JFileChooser fc = new JFileChooser(dir);
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fc.setMultiSelectionEnabled(false);//from w ww  . j  a  va  2  s  . c  o  m
    fc.setAcceptAllFileFilterUsed(false);
    fc.addChoosableFileFilter(new FileFilter() {

        @Override
        public String getDescription() {
            return description;
        }

        @Override
        public boolean accept(final File f) {
            return f.isDirectory() || FilenameUtils.isExtension(f.getName(), extension);
        }
    });
    return fc;
}

From source file:br.com.elotech.sits.config.dialog.ChooseFileDialog.java

private FileFilter buildKeyStoreFileFilter() {
    return new FileFilter() {

        private final List<String> ACEITAVEIS = Arrays.asList(new String[] { ".p12", ".pfx" });

        @Override// ww w .j a va  2 s  .co m
        public String getDescription() {
            return "Arquivos de informaes pessoais";
        }

        @Override
        public boolean accept(File f) {

            if (f.isDirectory()) {

                return true;

            } else if (option == ChooseOption.FILE) {

                for (String aceitavel : ACEITAVEIS) {
                    if (f.getName().toLowerCase().endsWith(aceitavel)) {
                        return true;
                    }
                }

            }

            return false;
        }

    };
}

From source file:org.obiba.onyx.jade.instrument.summitdoppler.VantageABIInstrumentRunner.java

public VantageABIInstrumentRunner() throws Exception {
    super();// w w  w . ja  v  a2s . com

    // Initialize interface components.
    saveButton = new JButton();
    saveButton.setMnemonic('S');
    // saveDataBtn.setEnabled(false);

    fileChooser = new JFileChooser();
    fileChooser.setFileFilter(new FileFilter() {

        @Override
        public String getDescription() {
            return "Vantage ABI (.ABI)";
        }

        @Override
        public boolean accept(File f) {
            return f.isDirectory() || f.getName().endsWith(".ABI");
        }
    });

    // Initialize interface components size
    appWindowWidth = 300;
    appWindowHeight = 175;
}

From source file:de.peterspan.csv2db.ui.FileSelectionPanel.java

private JFileChooser getFileChooser() {
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fc.setAcceptAllFileFilterUsed(false);

    fc.addChoosableFileFilter(new FileFilter() {

        @Override/*from w w  w  . java 2 s.  co m*/
        public String getDescription() {
            return "CSV Files";
        }

        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            }

            return FilenameUtils.isExtension(file.getName().toLowerCase(), "csv");
        }
    });

    return fc;
}

From source file:eu.delving.sip.actions.MediaImportAction.java

private void prepareChooser() {
    File directory = new File(sipModel.getPreferences().get(RECENT_DIR, System.getProperty("user.home")));
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setCurrentDirectory(directory.getParentFile());
    chooser.setFileFilter(new FileFilter() {
        @Override/*from   www .ja  va2s  . co m*/
        public boolean accept(File file) {
            return file.isDirectory();
        }

        @Override
        public String getDescription() {
            return "Directories";
        }
    });
    chooser.setMultiSelectionEnabled(false);
}

From source file:com.diversityarrays.kdxplore.vistool.VisToolbarFactory.java

static public VisToolToolBar create(final String title, final JComponent comp, final Closure<File> snapshotter,
        final VisToolDataProvider visToolDataProvider, boolean floatable, final String[] imageSuffixes) {
    Window window = GuiUtil.getOwnerWindow(comp);

    boolean anyButtons = false;

    final JCheckBox keepOnTop;

    if (window == null) {
        keepOnTop = null;// www. j a v  a  2  s  . c om
    } else {
        anyButtons = true;
        keepOnTop = new JCheckBox(Msg.OPTION_KEEP_ON_TOP(), true);

        keepOnTop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                window.setAlwaysOnTop(keepOnTop.isSelected());
            }
        });
        window.setAlwaysOnTop(keepOnTop.isSelected());

        //         buttons.add(keepOnTop);

        final PropertyChangeListener alwaysOnTopListener = new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                keepOnTop.setSelected(window.isAlwaysOnTop());
            }
        };
        window.addPropertyChangeListener(PROPERTY_ALWAYS_ON_TOP, alwaysOnTopListener);

        window.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                window.removeWindowListener(this);
                window.removePropertyChangeListener(PROPERTY_ALWAYS_ON_TOP, alwaysOnTopListener);
            }
        });
    }

    final JButton cameraButton;
    if (snapshotter == null) {
        cameraButton = null;
    } else {
        Action cameraAction = new AbstractAction(Msg.ACTION_SNAPSHOT()) {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (chooser == null) {
                    chooser = new JFileChooser();
                    chooser.setFileFilter(new FileFilter() {
                        @Override
                        public boolean accept(File f) {
                            if (!f.isFile()) {
                                return true;
                            }
                            String loname = f.getName().toLowerCase();
                            for (String sfx : imageSuffixes) {
                                if (loname.endsWith(sfx)) {
                                    return true;
                                }
                            }
                            return false;
                        }

                        @Override
                        public String getDescription() {
                            return Msg.DESC_IMAGE_FILE();
                        }
                    });
                    chooser.setMultiSelectionEnabled(false);
                    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                }

                if (JFileChooser.APPROVE_OPTION == chooser.showSaveDialog(comp)) {
                    File file = chooser.getSelectedFile();
                    snapshotter.execute(file);
                }
            }
        };

        ImageIcon icon = loadIcon("camera-24.png"); //$NON-NLS-1$
        if (icon != null) {
            cameraAction.putValue(Action.SMALL_ICON, icon);
            cameraAction.putValue(Action.NAME, null);
        }

        anyButtons = true;
        cameraButton = new JButton(cameraAction);
    }

    final JButton refreshButton;
    if (visToolDataProvider == null) {
        refreshButton = null;
    } else {
        anyButtons = true;

        refreshButton = new JButton(Msg.ACTION_REFRESH());

        ImageIcon icon = loadIcon("refresh-24.png"); //$NON-NLS-1$
        if (icon != null) {
            refreshButton.setIcon(icon);
            // don't remove the name
        }

        refreshButton.setForeground(Color.RED);
        refreshButton.setEnabled(false);

        refreshButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (visToolDataProvider.refreshData()) {
                    refreshButton.setEnabled(false);
                }
            }
        });

        visToolDataProvider.addVisToolDataChangedListener(new VisToolDataChangedListener() {
            @Override
            public void visToolDataChanged(Object source) {
                refreshButton.setEnabled(true);
            }
        });
    }

    VisToolToolBar toolBar = null;

    if (anyButtons) {
        toolBar = new VisToolToolBar(keepOnTop, cameraButton, refreshButton);
        toolBar.setFloatable(floatable);
    }
    return toolBar;

}

From source file:eu.delving.sip.actions.ImportAction.java

private void prepareChooser(SipModel sipModel) {
    File directory = new File(sipModel.getPreferences().get(RECENT_DIR, System.getProperty("user.home")));
    chooser.setCurrentDirectory(directory);
    chooser.setFileFilter(new FileFilter() {
        @Override/*from  w  ww.j a va2 s.  c o m*/
        public boolean accept(File file) {
            return file.isFile() && (file.getName().endsWith(".xml") || file.getName().endsWith(".xml.gz")
                    || file.getName().endsWith(".xml.zip") || file.getName().endsWith(".csv"));
        }

        @Override
        public String getDescription() {
            return "Files ending with .xml, .xml.gz, .xml.zip, or .csv";
        }
    });
    chooser.setMultiSelectionEnabled(false);
}

From source file:ch.tatool.app.export.FileDataExporter.java

private String getStorageDirectory(Component parentFrame) {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileFilter(new FileFilter() {

        @Override/*from  w w w .  j  av a 2  s. com*/
        public String getDescription() {
            return "CSV (*.csv)";
        }

        @Override
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }

            String extension = getExtension(f);
            if (extension != null) {
                if (extension.equals("csv")) {
                    return true;
                } else {
                    return false;
                }
            }

            return false;

        }
    });
    chooser.setAcceptAllFileFilterUsed(false);
    // chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setMultiSelectionEnabled(false);
    int returnVal = chooser.showSaveDialog(parentFrame);
    if (returnVal == JFileChooser.APPROVE_OPTION) {

        File f = chooser.getSelectedFile();
        String filename = "";
        if (f == null) {
            throw new RuntimeException();
        }
        String extension = getExtension(f);
        if (extension == null || !getExtension(f).equals("csv")) {
            filename = f + ".csv";
        } else {
            filename = f.getPath();
        }
        return filename;
    } else {
        return null;
    }
}