Example usage for javax.swing JFileChooser setFileFilter

List of usage examples for javax.swing JFileChooser setFileFilter

Introduction

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

Prototype

@BeanProperty(preferred = true, description = "Sets the File Filter used to filter out files of type.")
public void setFileFilter(FileFilter filter) 

Source Link

Document

Sets the current file filter.

Usage

From source file:app.RunApp.java

/**
 * Save co-ocurrence graph as image//from   w  ww .  j a v a2s  .co m
 * 
 * @throws AWTException
 * @throws IOException 
 */
private void saveCoocurrenceGraph() throws AWTException, IOException {
    BufferedImage image = new Robot().createScreenCapture(new Rectangle(
            panelCoOcurrenceRight.getLocationOnScreen().x, panelCoOcurrenceRight.getLocationOnScreen().y,
            panelCoOcurrenceRight.getWidth(), panelCoOcurrenceRight.getHeight()));

    JFileChooser fc = new JFileChooser();

    FileNameExtensionFilter fname1 = new FileNameExtensionFilter(".png", "png");

    fc.removeChoosableFileFilter(fc.getChoosableFileFilters()[0]);

    fc.setFileFilter(fname1);
    fc.addChoosableFileFilter(fname1);

    int returnVal = fc.showSaveDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = new File(fc.getSelectedFile().getAbsolutePath() + ".png");

        ImageIO.write(image, "png", file);

        JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
        Toolkit.getDefaultToolkit().beep();
    }
}

From source file:app.RunApp.java

/**
 * Action of Choose file button on Principal tab
 * //w  w  w  . ja v a 2  s  .co  m
 * @param evt Event
 */
private void buttonChooseFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonChooseFileActionPerformed
    final JFileChooser jfile1 = new JFileChooser();
    jfile1.setLocale(Locale.UK);
    jfile1.repaint();
    FileNameExtensionFilter fname = new FileNameExtensionFilter(".arff", "arff");
    jfile1.setFileFilter(fname);

    final boolean deleteXML = false;

    final int returnVal = jfile1.showOpenDialog(this);

    progressBar.setIndeterminate(true);
    progressFrame.setVisible(true);
    progressFrame.repaint();

    new Thread(new Runnable() {
        @Override
        public void run() {
            // do the long-running work here
            loadDataset(returnVal, jfile1, deleteXML);
            // at the end:
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    progressBar.setIndeterminate(false);
                    progressFrame.setVisible(false);
                    progressFrame.repaint();
                }//run
            }); //invokeLater
        }
    }).start();
}

From source file:com.monead.semantic.workbench.SemanticWorkbench.java

/**
 * Allow the user to select a file, which is expected to be an ontology, and
 * then load the file.//from w  w  w.  ja  v  a  2  s  .  com
 */
private void openOntologyFile() {
    JFileChooser fileChooser;
    FileFilter defaultFileFilter = null;
    FileFilter preferredFileFilter = null;
    File chosenFile;

    if (lastDirectoryUsed == null) {
        lastDirectoryUsed = new File(".");
    }

    fileChooser = new JFileChooser(lastDirectoryUsed);

    for (FileFilterDefinition filterDefinition : FileFilterDefinition.values()) {
        if (filterDefinition.name().startsWith("ONTOLOGY")) {
            final FileFilter fileFilter = new SuffixFileFilter(filterDefinition.description(),
                    filterDefinition.acceptedSuffixes());
            if (filterDefinition.isPreferredOption()) {
                preferredFileFilter = fileFilter;
            }
            fileChooser.addChoosableFileFilter(fileFilter);
            if (filterDefinition.description().equals(latestChosenRdfFileFilterDescription)) {
                defaultFileFilter = fileFilter;
            }
        }
    }

    if (defaultFileFilter != null) {
        fileChooser.setFileFilter(defaultFileFilter);
    } else if (latestChosenRdfFileFilterDescription != null
            && latestChosenRdfFileFilterDescription.startsWith("All")) {
        fileChooser.setFileFilter(fileChooser.getAcceptAllFileFilter());
    } else if (preferredFileFilter != null) {
        fileChooser.setFileFilter(preferredFileFilter);
    }

    fileChooser.setDialogTitle("Open Assertions File");
    fileChooser.showOpenDialog(this);

    try {
        latestChosenRdfFileFilterDescription = fileChooser.getFileFilter().getDescription();
    } catch (Throwable throwable) {
        LOGGER.warn("Unable to determine which ontology file filter was chosen", throwable);
    }

    chosenFile = fileChooser.getSelectedFile();

    if (chosenFile != null) {
        setupToLoadOntologyFile(new FileSource(chosenFile));
    }
}

From source file:com.monead.semantic.workbench.SemanticWorkbench.java

/**
 * Allow the user to select a file, which is expected to be an ontology, and
 * then load the file./*from  w  ww. j  ava 2 s .co m*/
 */
private void openSparqlQueryFile() {
    JFileChooser fileChooser;
    FileFilter defaultFileFilter = null;
    FileFilter preferredFileFilter = null;
    File chosenFile;

    if (lastDirectoryUsed == null) {
        lastDirectoryUsed = new File(".");
    }

    fileChooser = new JFileChooser(lastDirectoryUsed);

    for (FileFilterDefinition filterDefinition : FileFilterDefinition.values()) {
        if (filterDefinition.name().startsWith("SPARQL")) {
            final FileFilter fileFilter = new SuffixFileFilter(filterDefinition.description(),
                    filterDefinition.acceptedSuffixes());
            if (filterDefinition.isPreferredOption()) {
                preferredFileFilter = fileFilter;
            }
            fileChooser.addChoosableFileFilter(fileFilter);
            if (filterDefinition.description().equals(latestChosenSparqlFileFilterDescription)) {
                defaultFileFilter = fileFilter;
            }
        }
    }

    if (defaultFileFilter != null) {
        fileChooser.setFileFilter(defaultFileFilter);
    } else if (latestChosenSparqlFileFilterDescription != null
            && latestChosenSparqlFileFilterDescription.startsWith("All")) {
        fileChooser.setFileFilter(fileChooser.getAcceptAllFileFilter());
    } else if (preferredFileFilter != null) {
        fileChooser.setFileFilter(preferredFileFilter);
    }

    fileChooser.setDialogTitle("Open SPARQL Query File");
    fileChooser.showOpenDialog(this);

    try {
        latestChosenSparqlFileFilterDescription = fileChooser.getFileFilter().getDescription();
    } catch (Throwable throwable) {
        LOGGER.warn("Unable to determine which SPARQL file filter was chosen", throwable);
    }

    chosenFile = fileChooser.getSelectedFile();

    if (chosenFile != null) {
        loadSparqlQueryFile(chosenFile);
    }
}

From source file:com.monead.semantic.workbench.SemanticWorkbench.java

/**
 * Save the text from the SPARQL query text area to a file.
 * //w  ww  .java2 s  .  c  o m
 */
private void saveSparqlQueryToFile() {
    FileWriter out;
    JFileChooser fileChooser;
    FileFilter defaultFileFilter = null;
    FileFilter preferredFileFilter = null;
    File destinationFile;
    int choice;

    out = null;

    if (lastDirectoryUsed == null) {
        lastDirectoryUsed = new File(".");
    }

    fileChooser = new JFileChooser();

    for (FileFilterDefinition filterDefinition : FileFilterDefinition.values()) {
        if (filterDefinition.name().startsWith("SPARQL")) {
            final FileFilter fileFilter = new SuffixFileFilter(filterDefinition.description(),
                    filterDefinition.acceptedSuffixes());
            if (filterDefinition.isPreferredOption()) {
                preferredFileFilter = fileFilter;
            }
            fileChooser.addChoosableFileFilter(fileFilter);
            if (filterDefinition.description().equals(latestChosenSparqlFileFilterDescription)) {
                defaultFileFilter = fileFilter;
            }
        }
    }

    if (defaultFileFilter != null) {
        fileChooser.setFileFilter(defaultFileFilter);
    } else if (latestChosenSparqlFileFilterDescription != null
            && latestChosenSparqlFileFilterDescription.startsWith("All")) {
        fileChooser.setFileFilter(fileChooser.getAcceptAllFileFilter());
    } else if (preferredFileFilter != null) {
        fileChooser.setFileFilter(preferredFileFilter);
    }

    if (sparqlQueryFile != null) {
        fileChooser.setSelectedFile(sparqlQueryFile);
    } else {
        fileChooser.setSelectedFile(lastDirectoryUsed);
    }

    fileChooser.setDialogTitle("Save SPARQL Query to File");
    choice = fileChooser.showSaveDialog(this);

    try {
        latestChosenSparqlFileFilterDescription = fileChooser.getFileFilter().getDescription();
    } catch (Throwable throwable) {
        LOGGER.warn("Unable to determine which SPARQL file filter was chosen", throwable);
    }

    destinationFile = fileChooser.getSelectedFile();

    // Did not click save, did not select a file or chose a directory
    // So do not write anything
    if (choice != JFileChooser.APPROVE_OPTION || destinationFile == null
            || (destinationFile.exists() && !destinationFile.isFile())) {
        return; // EARLY EXIT!
    }

    // Adjust file suffix if necessary
    final FileFilter fileFilter = fileChooser.getFileFilter();
    if (fileFilter != null && fileFilter instanceof SuffixFileFilter
            && !fileChooser.getFileFilter().accept(destinationFile)) {
        destinationFile = ((SuffixFileFilter) fileFilter).makeWithPrimarySuffix(destinationFile);
    }

    if (okToOverwriteFile(destinationFile)) {

        LOGGER.info("Write SPARQL query to file, " + destinationFile);

        try {
            out = new FileWriter(destinationFile, false);

            if (sparqlServiceUrl.getSelectedIndex() != 0) {

                out.write(
                        "# " + SPARQL_QUERY_SAVE_SERVICE_URL_PARAM + sparqlServiceUrl.getSelectedItem() + "\n");
            } else {
                out.write("# " + SPARQL_QUERY_SAVE_SERVICE_URL_PARAM
                        + SPARQL_QUERY_SAVE_SERVICE_URL_VALUE_FOR_NO_SERVICE_URL + "\n");
            }

            if (defaultGraphUri.getText().trim().length() > 0) {
                out.write("# " + SPARQL_QUERY_SAVE_SERVICE_DEFAULT_GRAPH_PARAM
                        + defaultGraphUri.getText().trim() + "\n");
            }
            out.write(sparqlInput.getText());
            setSparqlQueryFile(destinationFile);
        } catch (IOException ioExc) {
            final String errorMessage = "Unable to write to file: " + destinationFile;
            LOGGER.error(errorMessage, ioExc);
            throw new RuntimeException(errorMessage, ioExc);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (Throwable throwable) {
                    final String errorMessage = "Failed to close output file: " + destinationFile;
                    LOGGER.error(errorMessage, throwable);
                    throw new RuntimeException(errorMessage, throwable);
                }
            }
            setTitle();
        }
    }
}

From source file:app.RunApp.java

/**
 * Action of Save table button in MVML tab
 * //from  w  w w .  j ava 2 s.c  o m
 * @param evt Event
 */
private void buttonSaveTableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSaveTableActionPerformed
    if (jTable2.getRowCount() == 0 || dataset == null) {
        JOptionPane.showMessageDialog(null, "The table is empty.", "Error", JOptionPane.ERROR_MESSAGE);
        return;
    }

    JFileChooser fc = new JFileChooser();

    //FileNameExtensionFilter fname = new FileNameExtensionFilter(".xls", "xls"); 
    FileNameExtensionFilter fname1 = new FileNameExtensionFilter(".csv", "csv");

    //Remove default
    fc.removeChoosableFileFilter(fc.getChoosableFileFilters()[0]);

    fc.setFileFilter(fname1);

    int returnVal = fc.showSaveDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        FileFilter f1 = fc.getFileFilter();

        if (f1.getDescription().equals(".csv")) {
            try {
                String path = file.getAbsolutePath() + ".csv";

                BufferedWriter bw = new BufferedWriter(new FileWriter(path));
                PrintWriter wr = new PrintWriter(bw);

                ResultsIOUtils.saveMVTableCsv(wr, jTable2, views, dataset);

                wr.close();
                bw.close();

                JOptionPane.showMessageDialog(null, "File saved.", "Successful",
                        JOptionPane.INFORMATION_MESSAGE);
            } catch (IOException | HeadlessException e1) {
                JOptionPane.showMessageDialog(null, "File not saved correctly.", "Error",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}

From source file:app.RunApp.java

/**
 * Action for Export button/*  ww w  .jav  a2  s.  co m*/
 * 
 * @param evt Event
 * @param jtable Table
 */
private void buttonExportActionPerformed(java.awt.event.ActionEvent evt, JTable jtable) {
    if (jtable.getRowCount() == 0 || dataset == null) {
        JOptionPane.showMessageDialog(null, "The table is empty.", "Error", JOptionPane.ERROR_MESSAGE);
        return;
    }

    JFileChooser fc = new JFileChooser();

    //FileNameExtensionFilter fname = new FileNameExtensionFilter(".xls", "xls"); 
    FileNameExtensionFilter fname1 = new FileNameExtensionFilter(".csv", "csv");

    //Remove default
    fc.removeChoosableFileFilter(fc.getChoosableFileFilters()[0]);

    fc.setFileFilter(fname1);

    int returnVal = fc.showSaveDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        FileFilter f1 = fc.getFileFilter();

        if (f1.getDescription().equals(".csv")) {
            try {
                String path = file.getAbsolutePath() + ".csv";

                BufferedWriter bw = new BufferedWriter(new FileWriter(path));
                PrintWriter wr = new PrintWriter(bw);

                switch (comboBoxLabelsInformation.getSelectedIndex()) {
                case 1:
                    ResultsIOUtils.saveTableLabelsetsFrequencyCsv(wr, jtable, labelsetStringsByFreq);
                    break;
                case 6:
                    ResultsIOUtils.saveTableLabelsetsIRCsv(wr, jtable, labelsetStringByIR);
                    break;
                default:
                    ResultsIOUtils.saveTableCsv(wr, jtable);
                    break;
                }

                wr.close();
                bw.close();

                JOptionPane.showMessageDialog(null, "File saved.", "Successful",
                        JOptionPane.INFORMATION_MESSAGE);
            } catch (IOException | HeadlessException e1) {
                JOptionPane.showMessageDialog(null, "File not saved correctly.", "Error",
                        JOptionPane.ERROR_MESSAGE);
            }

        }
    }
}

From source file:app.RunApp.java

/**
 * Action for Export button//  w  ww  .  java 2  s .co  m
 * 
 * @param evt Event
 * @param jtable Table
 * @param columns Columns
 * @param table Table type
 */
private void buttonExportActionPerformed(java.awt.event.ActionEvent evt, JTable jtable, JTable columns,
        String table) {
    if (jtable.getRowCount() == 0 || dataset == null) {
        JOptionPane.showMessageDialog(null, "The table is empty.", "Error", JOptionPane.ERROR_MESSAGE);
        return;
    }

    JFileChooser fc = new JFileChooser();

    //FileNameExtensionFilter fname = new FileNameExtensionFilter(".xls", "xls"); 
    FileNameExtensionFilter fname1 = new FileNameExtensionFilter(".csv", "csv");

    //Remove default
    fc.removeChoosableFileFilter(fc.getChoosableFileFilters()[0]);

    fc.setFileFilter(fname1);

    int returnVal = fc.showSaveDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        FileFilter f1 = fc.getFileFilter();

        //Saving csv chi_phi
        if (f1.getDescription().equals(".csv")) {
            BufferedWriter bw;
            PrintWriter wr;

            try {
                String path = file.getAbsolutePath() + ".csv";
                bw = new BufferedWriter(new FileWriter(path));
                wr = new PrintWriter(bw);

                switch (table) {
                case "ChiPhi":
                    ResultsIOUtils.saveChiPhiTableCsv(wr, chiPhiCoefficients, dataset.getLabelNames());
                    break;
                case "Coocurrence":
                    ResultsIOUtils.saveCoocurrenceTableCsv(wr, coocurrenceCoefficients,
                            dataset.getLabelNames());
                    break;
                case "Heatmap":
                    ResultsIOUtils.saveHeatmapTableCsv(wr, heatmapCoefficients, dataset.getLabelNames());
                    break;
                default:
                    JOptionPane.showMessageDialog(null, "File not saved correctly.", "Error",
                            JOptionPane.ERROR_MESSAGE);
                    break;
                }

                wr.close();
                bw.close();

                JOptionPane.showMessageDialog(null, "File saved.", "Successful",
                        JOptionPane.INFORMATION_MESSAGE);
            } catch (IOException | HeadlessException e1) {
                JOptionPane.showMessageDialog(null, "File not saved correctly.", "Error",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}

From source file:app.RunApp.java

/**
 * Action for Save button from multiple datasets tab
 * /* ww  w  . j a v  a 2s.c  om*/
 * @param evt Event
 * @param jtable Table
 * @throws IOException 
 */
private void buttonSaveActionPerformedMulti(java.awt.event.ActionEvent evt, JTable jtable) throws IOException {
    ArrayList<String> metricsList = getSelectedMetricsMulti(jtable);

    if (listDatasets == null || listDatasets.isEmpty() || datasetNames.isEmpty()) {
        JOptionPane.showMessageDialog(null, "You must load a dataset.", "Warning", JOptionPane.ERROR_MESSAGE);
        return;
    }

    //JFileChooser save
    JFileChooser fc = new JFileChooser();

    FileNameExtensionFilter fname = new FileNameExtensionFilter(".txt", "txt");
    FileNameExtensionFilter fname2 = new FileNameExtensionFilter(".csv", "csv");
    FileNameExtensionFilter fname3 = new FileNameExtensionFilter(".arff", ".arff");
    FileNameExtensionFilter fname4 = new FileNameExtensionFilter(".tex", ".tex");

    //Remove default
    fc.removeChoosableFileFilter(fc.getChoosableFileFilters()[0]);

    fc.addChoosableFileFilter(fname);
    fc.addChoosableFileFilter(fname2);
    fc.addChoosableFileFilter(fname3);
    fc.addChoosableFileFilter(fname4);

    fc.setFileFilter(fname);

    int returnVal = fc.showSaveDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        FileFilter f1 = fc.getFileFilter();

        String path;
        BufferedWriter bw;
        PrintWriter wr;

        switch (f1.getDescription()) {
        case ".txt":
            path = file.getAbsolutePath() + ".txt";
            bw = new BufferedWriter(new FileWriter(path));
            wr = new PrintWriter(bw);
            ResultsIOUtils.saveMultiMetricsTxt(wr, metricsList, datasetNames, tableMetricsMulti);
            wr.close();
            bw.close();
            JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
            break;
        case ".tex":
            path = file.getAbsolutePath() + ".tex";
            bw = new BufferedWriter(new FileWriter(path));
            wr = new PrintWriter(bw);
            ResultsIOUtils.saveMultiMetricsTex(wr, metricsList, datasetNames, tableMetricsMulti);
            wr.close();
            bw.close();
            JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
            break;
        case ".csv":
            path = file.getAbsolutePath() + ".csv";
            bw = new BufferedWriter(new FileWriter(path));
            wr = new PrintWriter(bw);
            ResultsIOUtils.saveMultiMetricsCsv(wr, metricsList, datasetNames, tableMetricsMulti);
            wr.close();
            bw.close();
            JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
            break;
        case ".arff":
            path = file.getAbsolutePath() + ".arff";
            bw = new BufferedWriter(new FileWriter(path));
            wr = new PrintWriter(bw);
            ResultsIOUtils.saveMultiMetricsArff(wr, metricsList, datasetNames, tableMetricsMulti);
            wr.close();
            bw.close();
            JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
            break;
        default:
            break;
        }

        Toolkit.getDefaultToolkit().beep();
    }
}

From source file:app.RunApp.java

/**
 * Action for Save button from principal tab
 * /*from w w w .  j a  va 2s .  c o m*/
 * @param evt Event
 * @param jtable Table
 * @throws IOException 
 */
private void buttonSaveActionPerformedPrincipal(java.awt.event.ActionEvent evt, JTable jtable)
        throws IOException {
    ArrayList<String> metricsList = getMetricsSelectedPrincipal(jtable);

    if (dataset == null) {
        JOptionPane.showMessageDialog(null, "You must load a dataset.", "Warning", JOptionPane.ERROR_MESSAGE);
        return;
    }

    JFileChooser fc = new JFileChooser();

    // extension txt
    FileNameExtensionFilter fname = new FileNameExtensionFilter(".txt", "txt");
    FileNameExtensionFilter fname2 = new FileNameExtensionFilter(".csv", "csv");
    FileNameExtensionFilter fname3 = new FileNameExtensionFilter(".arff", ".arff");
    FileNameExtensionFilter fname4 = new FileNameExtensionFilter(".tex", ".tex");

    //Remove default
    fc.removeChoosableFileFilter(fc.getChoosableFileFilters()[0]);

    fc.addChoosableFileFilter(fname);
    fc.addChoosableFileFilter(fname2);
    fc.addChoosableFileFilter(fname3);
    fc.addChoosableFileFilter(fname4);

    fc.setFileFilter(fname);

    int returnVal = fc.showSaveDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        FileFilter f1 = fc.getFileFilter();

        String path;
        BufferedWriter bw;
        PrintWriter wr;

        switch (f1.getDescription()) {
        case ".txt":
            path = file.getAbsolutePath() + ".txt";
            bw = new BufferedWriter(new FileWriter(path));
            wr = new PrintWriter(bw);
            ResultsIOUtils.saveMetricsTxt(wr, metricsList, dataset, tableMetrics);
            wr.close();
            bw.close();
            JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
            break;
        case ".tex":
            path = file.getAbsolutePath() + ".tex";
            bw = new BufferedWriter(new FileWriter(path));
            wr = new PrintWriter(bw);
            ResultsIOUtils.saveMetricsTex(wr, metricsList, tableMetrics);
            wr.close();
            bw.close();
            JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
            break;
        case ".csv":
            path = file.getAbsolutePath() + ".csv";
            bw = new BufferedWriter(new FileWriter(path));
            wr = new PrintWriter(bw);
            ResultsIOUtils.saveMetricsCsv(wr, metricsList, dataset, tableMetrics);
            wr.close();
            bw.close();
            JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
            break;
        case ".arff":
            path = file.getAbsolutePath() + ".arff";
            bw = new BufferedWriter(new FileWriter(path));
            wr = new PrintWriter(bw);
            ResultsIOUtils.saveMetricsArff(wr, metricsList, dataset, tableMetrics);
            wr.close();
            bw.close();
            JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
            break;
        default:
            break;
        }

        Toolkit.getDefaultToolkit().beep();
    }
}