Example usage for javax.swing.filechooser FileNameExtensionFilter FileNameExtensionFilter

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

Introduction

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

Prototype

public FileNameExtensionFilter(String description, String... extensions) 

Source Link

Document

Creates a FileNameExtensionFilter with the specified description and file name extensions.

Usage

From source file:CompareFiles.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:

    String userDir = System.getProperty("user.home");
    JFileChooser folder = new JFileChooser(userDir + "/Desktop");
    folder.setFileSelectionMode(JFileChooser.FILES_ONLY);
    FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter("Excel Files  (*.xls)", "xls");
    folder.setFileFilter(xmlfilter);// w w  w  .j  av  a  2  s .  co  m
    //        folder.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int returnvalue = folder.showSaveDialog(this);

    File myfolder = null;
    if (returnvalue == JFileChooser.APPROVE_OPTION) {
        myfolder = folder.getSelectedFile();
        //            System.out.println(myfolder);         
    }

    if (myfolder != null) {
        JOptionPane.showMessageDialog(null, "The current choosen file directory is : " + myfolder);
        jLabel1.setText(myfolder.toString());
    }

}

From source file:userinterface.properties.GUIGraphHandler.java

public GUIGraphHandler(JFrame parent, GUIPlugin plug, boolean canDelete) {
    this.plug = plug;
    this.canDelete = canDelete;

    this.graphMenu = new JPopupMenu();
    this.backMenu = new JPopupMenu();

    initComponents();//from  www.  j  ava 2  s .  c o m

    pngFilter = new FileNameExtensionFilter("PNG files (*.png)", "png");
    jpgFilter = new FileNameExtensionFilter("JPEG files (*.jpg, *.jpeg)", "jpg", "jpeg");
    pdfFilter = new FileNameExtensionFilter("PDF files(*.pdf)", "pdf");
    epsFilter = new FileNameExtensionFilter("Encapsulated PostScript files (*.eps)", "eps");
    graFilter = new FileNameExtensionFilter("PRISM graph files (*.gra, *.xml)", "gra", "xml");
    matlabFilter = new FileNameExtensionFilter("Matlab files (*.m)", "m");
    gnuplotFilter = new FileNameExtensionFilter("GNU plot files (*gnuplot , *.gplot , *.gp , *.plt , *.gpi)",
            "gnuplot");

    models = new ArrayList<JPanel>();
    options = new ArrayList<GraphOptions>();
}

From source file:com.genericworkflownodes.knime.nodes.io.OutputFileNodeDialog.java

public void createFileExtensionFilter() {
    final String mimeType = MIMEMap.getMIMEType(incomingFileExtension);
    MIMETypeEntry[] entries = MIMEMap.getAllTypes();
    final List<String> extensions = new ArrayList<String>();
    for (MIMETypeEntry entry : entries) {
        if (mimeType.equals(entry.getType())) {
            extensions.addAll(entry.getExtensions());
            break;
        }/*from  w ww .  j  av a2 s  . c  om*/
    }

    String[] exts = new String[extensions.size()];
    for (int i = 0; i < extensions.size(); ++i) {
        exts[i] = extensions.get(i).trim();
    }
    extensionFilter = new FileNameExtensionFilter(mimeType, exts);
}

From source file:it.unibas.spicygui.controllo.file.ActionOpenMappingTask.java

public Scenario openCompositionFile(String fileAbsoluteFile, File file, boolean isSelected) {
    Scenario scenario = null;/*from   w  ww .ja  va 2  s. c o  m*/
    try {
        java.util.Date date = new java.util.Date();
        System.out.println("Starting to load task at: " + new Timestamp(date.getTime()));
        if (logger.isDebugEnabled()) {
            logger.debug("Apro il file : " + fileAbsoluteFile);
        }
        //giannisk
        int scenarioNo = Scenarios.getNextFreeNumber();
        FileNameExtensionFilter tgdFilter = new FileNameExtensionFilter("TGD", "tgd");
        FileNameExtensionFilter xmlFilter = new FileNameExtensionFilter("XML", "xml");
        MappingTask mappingTask = null;
        DAOMappingTask daoMappingTask = new DAOMappingTask();
        if (xmlFilter.accept(file)) {
            mappingTask = daoMappingTask.loadMappingTask(scenarioNo, fileAbsoluteFile,
                    SpicyEngineConstants.LINES_BASED_MAPPING_TASK, false);
            enableActions();
            scenario = gestioneScenario(file, mappingTask, false, isSelected);
            this.actionViewSchema.performAction();
        } else if (tgdFilter.accept(file)) {
            mappingTask = daoMappingTask.loadMappingTask(scenarioNo, fileAbsoluteFile,
                    SpicyEngineConstants.TGD_BASED_MAPPING_TASK, false);
            enableActionsTGDs();
            scenario = gestioneScenario(file, mappingTask, true, isSelected);
            this.actionViewSchema.setEnabled(false);
            Lookups.forPath("Azione").lookup(ActionViewTGDs.class).myPerformAction(scenario);
            Lookups.forPath("Azione").lookup(ActionViewTGD.class).myPerformAction(scenario);
        }
        //giannisk open scenarios on the tree panel when loading mapping tasks
        this.actionProjectTree.performAction();
        java.util.Date date2 = new java.util.Date();
        System.out.println("Task loaded at: " + new Timestamp(date2.getTime()));
    } catch (Exception ex) {
        logger.error(ex);
        Scenarios.releaseNumber();
        DialogDisplayer.getDefault()
                .notify(new NotifyDescriptor.Message(
                        NbBundle.getMessage(Costanti.class, Costanti.OPEN_ERROR) + " : " + ex.getMessage(),
                        DialogDescriptor.ERROR_MESSAGE));
        logger.error(ex);
    }
    return scenario;

}

From source file:jpad.MainEditor.java

public void openFile() {
    if (_isOSX || ostype == ostype.Linux || ostype == ostype.Other) {
        openFile_OSX_Nix();//from   ww w .j av a2  s  . com
    } else {
        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle("Open a Text File");
        fc.setFileFilter(new FileNameExtensionFilter("Plain Text Files", "txt"));
        int returnval = fc.showOpenDialog(this);
        if (returnval == 0) {
            hasSavedToFile = true;
            hasChanges = false;
            curFile = fc.getSelectedFile().getAbsolutePath();
            try (FileInputStream stream = new FileInputStream(curFile)) {
                String file = IOUtils.toString(stream);
                mainTextArea.setText(file);
                this.setTitle(String.format("JPad - %s", curFile));
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}

From source file:electroStaticUI.ElectroStaticUIContainer.java

private void buildFileMenu() {
    //create Exit menu item
    exitItem = new JMenuItem("Exit");
    exitItem.setMnemonic(KeyEvent.VK_X);
    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);/*from ww w  .ja  v a2s  .c om*/
        }
    });

    //open
    open = new JMenuItem("Open");
    open.setMnemonic(KeyEvent.VK_P);
    open.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser fChooser = new JFileChooser();
            int status = fChooser.showSaveDialog(null);
        }
    });

    //close
    close = new JMenuItem("Close");
    close.setMnemonic(KeyEvent.VK_C);

    //export
    export = new JMenuItem("Export");
    export.setMnemonic(KeyEvent.VK_E);
    export.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser fChooser = new JFileChooser();
            fChooser.setMultiSelectionEnabled(false);
            fChooser.setAcceptAllFileFilterUsed(false);
            FileNameExtensionFilter svgFilter = new FileNameExtensionFilter("Save Vector Plot(svg)", ".svg");
            FileNameExtensionFilter pngFilter = new FileNameExtensionFilter("Save Voltage Surface Plot(png)",
                    ".png");
            fChooser.addChoosableFileFilter(svgFilter);
            fChooser.addChoosableFileFilter(pngFilter);
            fChooser.setFileFilter(svgFilter);
            int status = fChooser.showSaveDialog(rootPane);
            if (status == JFileChooser.APPROVE_OPTION) {
                if (fChooser.getFileFilter() == svgFilter) {
                    try {
                        saveAsName = fChooser.getSelectedFile().getCanonicalPath();
                        Save.saveChartToSVG(DefaultValues.getChartToSave(), saveAsName, displayPanel.getWidth(),
                                displayPanel.getHeight());
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                } else if (fChooser.getFileFilter() == pngFilter) {
                    try {
                        saveAsName = fChooser.getSelectedFile().getCanonicalPath();
                        Save.saveChart3dToPNG(DefaultValues.get3dChartToSave(), saveAsName);
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }
        }
    });

    //save
    save = new JMenuItem("Save");
    save.setMnemonic(KeyEvent.VK_S);
    save.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser fChooser = new JFileChooser();
            int status = fChooser.showSaveDialog(null);
            if (status == JFileChooser.APPROVE_OPTION) {
                try {
                    System.out.println("Height: " + displayPanel.getWidth());
                    System.out.println("Width: " + displayPanel.getHeight());
                    saveAsName = fChooser.getSelectedFile().getCanonicalPath();
                    Save.saveChartToSVG(DefaultValues.getChartToSave(), saveAsName, displayPanel.getWidth(),
                            displayPanel.getHeight());
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

        }
    });

    //saveAs
    saveAs = new JMenuItem("Save As");
    saveAs.setMnemonic(KeyEvent.VK_A);
    saveAs.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser fChooser = new JFileChooser();
            int status = fChooser.showSaveDialog(null);
            if (status == JFileChooser.APPROVE_OPTION)
                try {
                    System.out.println("Height: " + displayPanel.getWidth());
                    System.out.println("Width: " + displayPanel.getHeight());
                    saveAsName = fChooser.getSelectedFile().getCanonicalPath();
                    System.out.println(saveAsName);
                    Save.saveChartToSVG(DefaultValues.getChartToSave(), saveAsName, displayPanel.getWidth(),
                            displayPanel.getHeight());
                } catch (IOException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
            try {
                Save.saveChartToSVG(UserInput.getElectricFieldChart(), saveAsName, displayPanel.getWidth(),
                        displayPanel.getHeight());
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });

    //JMenu object for file menu
    fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);

    //fileMenu.add(newFile);
    //fileMenu.add(open);
    //fileMenu.add(close);
    fileMenu.add(export);
    //fileMenu.add(save);
    //fileMenu.add(saveAs);
    fileMenu.add(exitItem);
}

From source file:GraphUI.java

private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBtnActionPerformed
    int width = 640;
    int height = 480;
    fc = new JFileChooser();
    fc.addChoosableFileFilter(new FileNameExtensionFilter("PNG (*.png)", "png"));
    fc.setAcceptAllFileFilterUsed(false);
    fc.showSaveDialog(null);/*  w  ww  .ja va  2s .co  m*/

    String path = fc.getSelectedFile().getAbsolutePath() + ".png";
    String filename = fc.getSelectedFile().getName() + ".png";
    File imageFile = new File(path);

    /*
    System.out.println("getCurrentDirectory(): "
        + f.getCurrentDirectory());
    System.out.println("getSelectedFile() : "
        + f.getSelectedFile());
     */
    try {
        ChartUtilities.saveChartAsPNG(imageFile, chart, width, height);
        JOptionPane.showMessageDialog(null, "Graph saved as " + filename);
    } catch (IOException ex) {
        System.err.println(ex);
    }

}

From source file:view.MainFrame.java

private void menuItemOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemOpenActionPerformed
    // TODO add your handling code here:
    JFileChooser filech = new JFileChooser();
    filech.setFileSelectionMode(JFileChooser.FILES_ONLY);
    filech.setFileFilter(new FileNameExtensionFilter("WAV Files", "wav"));
    int ret = filech.showOpenDialog(this);

    if (ret == JFileChooser.APPROVE_OPTION) {
        try {/*from  w w  w.  j  a  va2 s. com*/
            wavFile = WavFile.openWavFile(filech.getSelectedFile());
            textArea.append(wavFile.getInfoString());
            samples = new double[wavFile.getNumChannels() * (int) wavFile.getSampleRate()];
            int nFrames = wavFile.readFrames(samples, (int) wavFile.getSampleRate());
            textArea.append(nFrames + " frames lidos.\n");

            double valuesX[] = new double[samples.length];
            for (int i = 0; i < samples.length; i++) {
                valuesX[i] = i;
            }

            showChart(wavFile.getNumChannels() * (int) wavFile.getSampleRate(), samples, valuesX, "Amostra",
                    "Amplitude", "Amplitude das Amostras do ?udio", "Audio");
        } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (WavFileException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:net.sf.mzmine.modules.visualization.ida.IDAPlot.java

@Override
public void actionPerformed(final ActionEvent event) {

    super.actionPerformed(event);

    final String command = event.getActionCommand();

    if ("SAVE_EMF".equals(command)) {

        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("EMF Image", "EMF");
        chooser.setFileFilter(filter);/*w  w  w .  jav a 2s. c  o m*/
        int returnVal = chooser.showSaveDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            String file = chooser.getSelectedFile().getPath();
            if (!file.toLowerCase().endsWith(".emf"))
                file += ".emf";

            int width = (int) this.getSize().getWidth();
            int height = (int) this.getSize().getHeight();

            // Save image
            SaveImage SI = new SaveImage(getChart(), file, width, height, FileType.EMF);
            new Thread(SI).start();

        }
    }

    if ("SAVE_EPS".equals(command)) {

        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("EPS Image", "EPS");
        chooser.setFileFilter(filter);
        int returnVal = chooser.showSaveDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            String file = chooser.getSelectedFile().getPath();
            if (!file.toLowerCase().endsWith(".eps"))
                file += ".eps";

            int width = (int) this.getSize().getWidth();
            int height = (int) this.getSize().getHeight();

            // Save image
            SaveImage SI = new SaveImage(getChart(), file, width, height, FileType.EPS);
            new Thread(SI).start();

        }

    }
}

From source file:edu.gcsc.vrl.jfreechart.JFExport.java

/**
 * Show dialog for exporting JFreechart object.
 *//*  w w w .j a v  a  2  s  .c  om*/
public void openExportDialog(JFreeChart jfreechart) {

    JFileChooser chooser = new JFileChooser();
    String path0;
    File file;

    chooser.addChoosableFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));
    chooser.addChoosableFileFilter(new FileNameExtensionFilter("JPEG Files", "jpg"));
    chooser.addChoosableFileFilter(new FileNameExtensionFilter("PNG Files", "png"));
    chooser.addChoosableFileFilter(new FileNameExtensionFilter("EPS Files", "eps"));
    chooser.addChoosableFileFilter(new FileNameExtensionFilter("SVG Files", "svg"));

    chooser.setFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));

    int returnVal = chooser.showSaveDialog(null);
    String fd = chooser.getFileFilter().getDescription();

    // Get selected FileFilter 
    String filter_extension = null;
    if (fd.equals("PNG Files")) {
        filter_extension = "png";
    }
    if (fd.equals("SVG Files")) {
        filter_extension = "svg";
    }
    if (fd.equals("JPEG Files")) {
        filter_extension = "jpg";
    }
    if (fd.equals("PDF Files")) {
        filter_extension = "pdf";
    }
    if (fd.equals("EPS Files")) {
        filter_extension = "eps";
    }

    // Cancel
    if (returnVal == JFileChooser.CANCEL_OPTION) {
        return;
    }

    // OK
    if (returnVal == JFileChooser.APPROVE_OPTION) {

        path0 = chooser.getSelectedFile().getAbsolutePath();
        //System.out.println(path0);
        try {
            file = new File(path0);
            // Get extension (if any)
            String ext = JFUtils.getExtension(file);
            // No extension -> use selected Filter
            if (ext == null) {
                file = new File(path0 + "." + filter_extension);
                ext = filter_extension;
            }

            // File exists - overwrite ?
            if (file.exists()) {
                Object[] options = { "OK", "CANCEL" };
                int answer = JOptionPane.showOptionDialog(null, "File exists - Overwrite ?", "Warning",
                        JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
                if (answer != JOptionPane.YES_OPTION) {
                    return;
                }
                //System.out.println(answer+"");
            }

            // Export file
            export(file, jfreechart);
        } catch (Exception f) {
            // I/O - Error
        }
    }
}