Example usage for javax.swing JFileChooser addChoosableFileFilter

List of usage examples for javax.swing JFileChooser addChoosableFileFilter

Introduction

In this page you can find the example usage for javax.swing JFileChooser addChoosableFileFilter.

Prototype

@BeanProperty(preferred = true, description = "Adds a filter to the list of user choosable file filters.")
public void addChoosableFileFilter(FileFilter filter) 

Source Link

Document

Adds a filter to the list of user choosable file filters.

Usage

From source file:Main.java

public void actionPerformed(ActionEvent e) {
    int retVal;/*  w w w.j  a  v  a 2s  .  c o m*/

    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());
}

From source file:MyFilterChooser.java

public MyFilterChooser() {
    super("Filter Test Frame");
    setSize(350, 200);/*from   w  w w. j a v a  2 s.  c  om*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container c = getContentPane();
    c.setLayout(new FlowLayout());

    JButton openButton = new JButton("Open");
    final JLabel statusbar = new JLabel("Output of your selection will go here");

    openButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            String[] pics = new String[] { "gif", "jpg", "tif" };
            String[] audios = new String[] { "au", "aiff", "wav" };
            JFileChooser chooser = new JFileChooser();
            chooser.addChoosableFileFilter(new SimpleFileFilter(pics, "Images (*.gif, *.jpg, *.tif)"));
            chooser.addChoosableFileFilter(new SimpleFileFilter(".MOV"));
            chooser.addChoosableFileFilter(new SimpleFileFilter(audios, "Sounds (*.aiff, *.au, *.wav)"));
            int option = chooser.showOpenDialog(MyFilterChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                if (chooser.getSelectedFile() != null)
                    statusbar.setText("You chose " + chooser.getSelectedFile().getName());
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

    c.add(openButton);
    c.add(statusbar);
    setVisible(true);
}

From source file:com.dfki.av.sudplan.vis.wiz.DataSourceSelectionPanel.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    JFileChooser jfc = new JFileChooser();
    jfc.addChoosableFileFilter(new FileNameExtensionFilter("ESRI Shapfile (*.shp)", "shp", "Shp", "SHP"));
    jfc.addChoosableFileFilter(new FileNameExtensionFilter("Zip file (*.zip)", "zip", "ZIP", "Zip"));
    XMLConfiguration xmlConfig = Configuration.getXMLConfiguration();
    String path = xmlConfig.getString("sudplan3D.working.dir");
    File dir;/*  w  w w. j a  v  a2s .co m*/
    if (path != null) {
        dir = new File(path);
        if (dir.exists()) {
            jfc.setCurrentDirectory(dir);
        }
    }
    int retValue = jfc.showOpenDialog(this);

    if (retValue == JFileChooser.APPROVE_OPTION) {
        File f = jfc.getSelectedFile();
        if (f != null) {
            jLabel1.setText(f.getAbsolutePath());
            file = f;
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("No shp file selected.");
        }
    }
    dir = jfc.getCurrentDirectory();
    path = dir.getAbsolutePath();
    xmlConfig.setProperty("sudplan3D.working.dir", path);
}

From source file:net.sf.clichart.main.ChartFrame.java

private void saveChart() {
    JFileChooser chooser = new JFileChooser(".");
    chooser.addChoosableFileFilter(new ExtensionFileFilter(".jpg", "JPEG files"));
    chooser.addChoosableFileFilter(new ExtensionFileFilter(".png", "PNG files"));

    if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
        File outputFile = chooser.getSelectedFile();

        if (outputFile.exists()) {
            int result = JOptionPane.showConfirmDialog(this, "File exists - overwrite?", "File exists",
                    JOptionPane.OK_CANCEL_OPTION);
            if (result != JOptionPane.OK_OPTION) {
                return;
            }/*from   www .j  a  v  a2 s.  c  o  m*/
        }

        System.err.println("Saving chart to " + outputFile.getPath());
        try {
            new ChartSaver(m_chart, m_initialWidth, m_initialHeight).saveChart(outputFile);
        } catch (ChartSaverException e) {
            JOptionPane.showMessageDialog(this, "Failed to save chart: " + e.getMessage());
        }
    }
}

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// www .ja  v  a 2  s.  c o  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:org.cytoscape.dyn.internal.graphMetrics.SaveChartDialog.java

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if (e.getSource() == cancelButton) {
        this.setVisible(false);
        this.dispose();
    } else if (e.getSource() == saveChartButton) {
        JFileChooser saveFileDialog = new JFileChooser();
        saveFileDialog
                .addChoosableFileFilter(new ExtensionFileFilter(".jpeg", ".jpg", "Jpeg images (.jpeg, .jpg)"));
        saveFileDialog.addChoosableFileFilter(
                new ExtensionFileFilter(".png", "Portable Network Graphic images (.png)"));
        saveFileDialog//  ww  w. ja  va  2 s.  c o m
                .addChoosableFileFilter(new ExtensionFileFilter(".svg", "Scalable Vector Graphics (.svg)"));
        int save = saveFileDialog.showSaveDialog(this);
        if (save == JFileChooser.APPROVE_OPTION) {
            File file = saveFileDialog.getSelectedFile();
            int width = ((SpinnerNumberModel) widthSpinner.getModel()).getNumber().intValue();
            int height = ((SpinnerNumberModel) heightSpinner.getModel()).getNumber().intValue();

            ExtensionFileFilter filter = null;
            try {
                filter = (ExtensionFileFilter) saveFileDialog.getFileFilter();
                if (!filter.hasExtension(file)) {
                    file = filter.appendExtension(file);
                }
            } catch (ClassCastException ex) {
                // Try to infer the type of file by its extension
                FileFilter[] filters = saveFileDialog.getChoosableFileFilters();
                for (int i = 0; i < filters.length; ++i) {
                    if (filters[i] instanceof ExtensionFileFilter) {
                        filter = (ExtensionFileFilter) filters[i];
                        if (filter.hasExtension(file)) {
                            break;
                        }
                        filter = null;
                    }
                }

                if (filter == null) {
                    // Could not infer the type
                    JOptionPane.showMessageDialog(null,
                            "File type not specified!\nWhen giving file name, please also select one of the supported file types.",
                            "Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            }
            // Save the chart to the specified file name
            try {
                String ext = filter.getExtension();
                if (ext.equals("jpeg")) {
                    ChartUtilities.saveChartAsJPEG(file, chart, width, height);
                } else if (ext.equals("png")) {
                    ChartUtilities.saveChartAsPNG(file, chart, width, height);
                } else {
                    VectorGraphics graphics = new SVGGraphics2D(file, new Dimension(width, height));
                    graphics.startExport();
                    chart.draw(graphics, new Rectangle2D.Double(0, 0, width, height));
                    graphics.endExport();
                }
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "An error occurred while creating or writing to the file.",
                        "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            this.setVisible(false);
            this.dispose();
        } else if (save == JFileChooser.ERROR_OPTION) {
            JOptionPane.showMessageDialog(null, "An error occurred while initializing the window.", "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}

From source file:calendarexportplugin.exporter.CalExporter.java

/**
 * Shows a file chooser for calendar Files.
 *
 * @return selected File/*  w  w w . ja v a 2  s  .  c o m*/
 * @param programs
 *          programs that are exported
 */
private File chooseFile(Program[] programs) {
    JFileChooser select = new JFileChooser();

    ExtensionFileFilter vCal = new ExtensionFileFilter(mExtension, mExtensionFilter);
    select.addChoosableFileFilter(vCal);
    String ext = "." + mExtension;

    if (mSavePath != null) {
        select.setSelectedFile(new File(mSavePath));
        select.setFileFilter(vCal);
    }

    // check if all programs have same title. if so, use as filename
    String fileName = programs[0].getTitle();
    for (int i = 1; i < programs.length; i++) {
        if (!programs[i].getTitle().equals(fileName)) {
            fileName = "";
        }
    }

    fileName = CalendarToolbox.cleanFilename(fileName);

    if (StringUtils.isNotEmpty(fileName)) {
        if (mSavePath == null) {
            mSavePath = "";
        }
        select.setSelectedFile(new File((new File(mSavePath).getParent()) + File.separator + fileName + ext));
    }

    if (select.showSaveDialog(
            CalendarExportPlugin.getInstance().getBestParentFrame()) == JFileChooser.APPROVE_OPTION) {

        String filename = select.getSelectedFile().getAbsolutePath();

        if (!filename.toLowerCase().endsWith(ext)) {
            if (filename.endsWith(".")) {
                filename = filename.substring(0, filename.length() - 1);
            }
            filename = filename + ext;
        }

        return new File(filename);
    }

    return null;
}

From source file:com.view.Main.java

private void filePathBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filePathBrowseButtonActionPerformed
    final JFileChooser fileChooser = new JFileChooser(".");
    final FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel files", "xlsx", "xls");
    fileChooser.addChoosableFileFilter(filter);
    fileChooser.setFileFilter(filter);/*from  ww w  . j av  a 2  s .c om*/
    int result = fileChooser.showDialog(this, "choose a file");
    if (result == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        filePathTextField.setText(selectedFile.getAbsolutePath());
    }

}

From source file:com.naval.gui.Gui.java

private void creerPartie() {
    final JFileChooser fc = new JFileChooser(Config.getRepTravail());

    fc.addChoosableFileFilter(new GameFileFilter("gm", "Description de partie"));
    fc.setAcceptAllFileFilterUsed(false);

    int returnVal = fc.showOpenDialog(frame);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();

        // TODO : load only if game is !=

        try {/*from ww w  .  jav  a 2 s .  c  o  m*/
            FileReader fr = new FileReader(file);
            partie = Partie.creer(fr);
            partie.save();
            hintBar.setText("Partie " + partie.nom + " cre avec succes");
        } catch (FileNotFoundException e) {
            hintBar.setText(e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            hintBar.setText(e.getMessage());
            e.printStackTrace();
        }
    }
}

From source file:com.naval.gui.Gui.java

private void chargerPartie() {
    final JFileChooser fc = new JFileChooser(Config.getRepTravail());

    fc.addChoosableFileFilter(new GameFileFilter("serial", "Sauv de partie"));
    fc.setAcceptAllFileFilterUsed(false);

    int returnVal = fc.showOpenDialog(frame);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();

        // TODO : load only if game is !=
        try {/* w w  w  . j  a  v  a 2  s.c  o  m*/
            partie = Partie.load(file.getAbsolutePath());

            menuFac.updateForLoad();
            update();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            hintBar.setText(e.getMessage());
        } catch (IOException e) {
            hintBar.setText(e.getMessage());
            e.printStackTrace();

        } catch (ClassNotFoundException e) {
            hintBar.setText(e.getMessage());
            e.printStackTrace();
        }
    }
    if (partie.ordres == null || partie.ordres.size() == 0) {
        for (Navire n : partie.navires) {
            // creation des 3 ordres pour le tour courant.
            partie.ordres.add(new Ordre(n.id, partie.minute));
            partie.ordres.add(new Ordre(n.id, partie.minute));
            partie.ordres.add(new Ordre(n.id, partie.minute));

        }
    }
}