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:org.zaproxy.zap.extension.customFire.PolicyManagerDialog.java

private JButton getExportButton() {
    if (this.exportButton == null) {
        this.exportButton = new JButton(
                Constant.messages.getString("customFire.custom.policymgr.button.export"));
        this.exportButton.setEnabled(false);
        this.exportButton.addActionListener(new ActionListener() {
            @Override//  ww  w  .j a  v a  2  s. c  om
            public void actionPerformed(ActionEvent e) {
                String name = (String) getParamsModel().getValueAt(getParamsTable().getSelectedRow(), 0);
                if (name != null) {
                    JFileChooser chooser = new JFileChooser(Constant.getPoliciesDir());
                    File file = new File(Constant.getZapHome(), name + PolicyManager.POLICY_EXTENSION);
                    chooser.setSelectedFile(file);

                    chooser.setFileFilter(new FileFilter() {
                        @Override
                        public boolean accept(File file) {
                            if (file.isDirectory()) {
                                return true;
                            } else if (file.isFile() && file.getName().endsWith(".policy")) {
                                return true;
                            }
                            return false;
                        }

                        @Override
                        public String getDescription() {
                            return Constant.messages.getString("file.format.zap.policy");
                        }
                    });
                    int rc = chooser.showSaveDialog(View.getSingleton().getMainFrame());
                    if (rc == JFileChooser.APPROVE_OPTION) {
                        file = chooser.getSelectedFile();
                        if (file == null) {
                            return;
                        }
                        try {
                            CustomScanPolicy policy = extension.getPolicyManager().getPolicy(name);
                            if (policy != null) {
                                extension.getPolicyManager().exportPolicy(policy, file);
                            }
                        } catch (ConfigurationException e1) {
                            logger.error(e1.getMessage(), e1);
                            View.getSingleton().showWarningDialog(
                                    Constant.messages.getString("customFire.custom.policy.load.error"));
                        }
                    }
                }
            }
        });
    }
    return this.exportButton;
}

From source file:org.zaproxy.zap.extension.customFire.TechnologyTreePanel.java

/**
 * To save tech settings// w  w w.j  a  va 2s .  com
 *  void `
 */
public void saveTechState() {

    //Do Tech ser
    JFileChooser chooser = new JFileChooser(Constant.getPoliciesDir());
    File file = new File(Constant.getZapHome(), "Tech.ser");
    chooser.setSelectedFile(file);

    chooser.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().endsWith(".ser")) {
                return true;
            }
            return false;
        }

        @Override
        public String getDescription() {
            return Constant.messages.getString("customFire.custom.file.format.csp.ser");
        }
    });
    int rc = chooser.showSaveDialog(View.getSingleton().getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        if (file == null) {
            return;
        }
        try {

            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(TechnologyTreePanel.this);
            oos.close();
            fos.close();
            View.getSingleton()
                    .showMessageDialog(Constant.messages.getString("customFire.custom.ser.saveTech.success"));

        } catch (IOException e1) {
            View.getSingleton()
                    .showWarningDialog(Constant.messages.getString("customFire.custom.ser.saveTech.error"));
            return;
        }
    }
    if (rc == JFileChooser.CANCEL_OPTION) {
        chooser.setVisible(false);
        return;
    }

}

From source file:org.zaproxy.zap.extension.dynssl.DynamicSSLPanel.java

/**
 * Import Root CA certificate from other ZAP configuration files.
 */// www . j a  v  a 2s.  co m
private void doImport() {
    if (checkExistingCertificate()) {
        // prevent overwriting
        return;
    }
    final JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fc.setMultiSelectionEnabled(false);
    fc.setSelectedFile(new File(CONFIGURATION_FILENAME));
    fc.setFileFilter(new FileFilter() {
        @Override
        public String getDescription() {
            // config.xml or *.pem files
            return Constant.messages.getString("dynssl.filter.file");
        }

        @Override
        public boolean accept(File f) {
            return f.getName().toLowerCase().endsWith(CONFIGURATION_FILENAME)
                    || f.getName().toLowerCase().endsWith("pem") || f.isDirectory();
        }
    });
    final int result = fc.showOpenDialog(this);
    final File f = fc.getSelectedFile();
    if (result == JFileChooser.APPROVE_OPTION && f.exists()) {
        if (logger.isInfoEnabled()) {
            logger.info("Loading Root CA certificate from " + f);
        }
        KeyStore ks = null;
        if (f.getName().toLowerCase().endsWith("pem")) {
            ks = convertPemFileToKeyStore(f.toPath());
        } else {
            try {
                final ZapXmlConfiguration conf = new ZapXmlConfiguration(f);
                final String rootcastr = conf.getString(DynSSLParam.PARAM_ROOT_CA);
                ks = SslCertificateUtils.string2Keystore(rootcastr);
            } catch (final Exception e) {
                logger.error("Error importing Root CA cert from config file:", e);
                JOptionPane.showMessageDialog(this,
                        Constant.messages.getString("dynssl.message1.filecouldntloaded"),
                        Constant.messages.getString("dynssl.message1.title"), JOptionPane.ERROR_MESSAGE);
            }
        }
        if (ks != null) {
            setRootca(ks);
        }

    }
}

From source file:org.zaproxy.zap.extension.history.PopupMenuExportURLs.java

private File getOutputFile() {

    JFileChooser chooser = new JFileChooser(extension.getModel().getOptionsParam().getUserDirectory());
    chooser.setFileFilter(new FileFilter() {
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().toLowerCase().endsWith(".txt")) {
                return true;
            } else if (file.isFile() && file.getName().toLowerCase().endsWith(".htm")) {
                return true;
            } else if (file.isFile() && file.getName().toLowerCase().endsWith(".html")) {
                return true;
            }//from   w w w . j  a  v  a  2 s .  co  m
            return false;
        }

        public String getDescription() {
            return Constant.messages.getString("file.format.textOrHtml");
        }
    });

    File file = null;
    int rc = chooser.showSaveDialog(extension.getView().getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        if (file == null) {
            return file;
        }
        extension.getModel().getOptionsParam().setUserDirectory(chooser.getCurrentDirectory());
        String fileName = file.getAbsolutePath().toLowerCase();
        if (!fileName.endsWith(".txt") && !fileName.endsWith(".htm") && !fileName.endsWith(".html")) {
            fileName += ".txt";
            file = new File(fileName);
        }
        return file;

    }
    return file;
}

From source file:org.zaproxy.zap.extension.soap.ExtensionImportWSDL.java

private ZapMenuItem getMenuImportLocalWSDL() {
    if (menuImportLocalWSDL == null) {
        menuImportLocalWSDL = new ZapMenuItem("soap.topmenu.tools.importWSDL",
                KeyStroke.getKeyStroke(KeyEvent.VK_I, Event.CTRL_MASK, false));
        menuImportLocalWSDL//  www .j av a 2s.  com
                .setToolTipText(Constant.messages.getString("soap.topmenu.tools.importWSDL.tooltip"));

        menuImportLocalWSDL.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                // Prompt for a WSDL file.
                final JFileChooser chooser = new JFileChooser(
                        Model.getSingleton().getOptionsParam().getUserDirectory());
                FileNameExtensionFilter filter = new FileNameExtensionFilter("WSDL File", "wsdl", "wsdl");
                chooser.setFileFilter(filter);
                int rc = chooser.showOpenDialog(View.getSingleton().getMainFrame());
                if (rc == JFileChooser.APPROVE_OPTION) {

                    Thread t = new Thread() {
                        @Override
                        public void run() {
                            this.setName(THREAD_PREFIX + threadId++);
                            parseWSDLFile(chooser.getSelectedFile());
                        }

                    };
                    t.start();
                }

            }
        });
    }
    return menuImportLocalWSDL;
}

From source file:org.zeromeaner.gui.reskin.StandaloneFrame.java

private void createCards() {
    introPanel = new StandaloneLicensePanel();
    content.add(introPanel, CARD_INTRO);

    playCard = new JPanel(new BorderLayout());
    content.add(playCard, CARD_PLAY);/*w w w  .j  a  v a  2 s .  c  o  m*/

    JPanel confirm = new JPanel(new GridBagLayout());
    JOptionPane option = new JOptionPane("A game is in open.  End this game?", JOptionPane.QUESTION_MESSAGE,
            JOptionPane.YES_NO_OPTION);
    option.addPropertyChangeListener(JOptionPane.VALUE_PROPERTY, new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (!CARD_PLAY_END.equals(currentCard))
                return;
            if (evt.getNewValue().equals(JOptionPane.YES_OPTION)) {
                gamePanel.shutdown();
                try {
                    gamePanel.shutdownWait();
                } catch (InterruptedException ie) {
                }
                contentCards.show(content, nextCard);
                currentCard = nextCard;
            }
            if (evt.getNewValue().equals(JOptionPane.NO_OPTION)) {
                contentCards.show(content, CARD_PLAY);
                currentCard = CARD_PLAY;
                playButton.setSelected(true);
            }

            ((JOptionPane) evt.getSource()).setValue(-1);
        }
    });
    GridBagConstraints cx = new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER,
            GridBagConstraints.NONE, new Insets(10, 10, 10, 10), 0, 0);
    confirm.add(option, cx);
    content.add(confirm, CARD_PLAY_END);

    content.add(new StandaloneModeselectPanel(), CARD_MODESELECT);

    netplayCard = new JPanel(new BorderLayout());
    netplayCard.add(netLobby, BorderLayout.SOUTH);
    content.add(netplayCard, CARD_NETPLAY);

    confirm = new JPanel(new GridBagLayout());
    option = new JOptionPane("A netplay game is open.  End this game and disconnect?",
            JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
    option.addPropertyChangeListener(JOptionPane.VALUE_PROPERTY, new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (!CARD_NETPLAY_END.equals(currentCard))
                return;
            if (evt.getNewValue().equals(JOptionPane.YES_OPTION)) {
                gamePanel.shutdown();
                try {
                    gamePanel.shutdownWait();
                } catch (InterruptedException ie) {
                }
                netLobby.disconnect();
                contentCards.show(content, nextCard);
                currentCard = nextCard;
            }
            if (evt.getNewValue().equals(JOptionPane.NO_OPTION)) {
                contentCards.show(content, CARD_NETPLAY);
                currentCard = CARD_NETPLAY;
                netplayButton.setSelected(true);
            }

            ((JOptionPane) evt.getSource()).setValue(-1);
        }
    });
    cx = new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,
            new Insets(10, 10, 10, 10), 0, 0);
    confirm.add(option, cx);
    content.add(confirm, CARD_NETPLAY_END);

    StandaloneKeyConfig kc = new StandaloneKeyConfig(this);
    kc.load(0);
    content.add(kc, CARD_KEYS_1P);
    kc = new StandaloneKeyConfig(this);
    kc.load(1);
    content.add(kc, CARD_KEYS_2P);

    StandaloneGameTuningPanel gt = new StandaloneGameTuningPanel();
    gt.load(0);
    content.add(gt, CARD_TUNING_1P);
    gt = new StandaloneGameTuningPanel();
    gt.load(1);
    content.add(gt, CARD_TUNING_2P);

    StandaloneAISelectPanel ai = new StandaloneAISelectPanel();
    ai.load(0);
    content.add(ai, CARD_AI_1P);
    ai = new StandaloneAISelectPanel();
    ai.load(1);
    content.add(ai, CARD_AI_2P);

    StandaloneGeneralConfigPanel gc = new StandaloneGeneralConfigPanel();
    gc.load();
    content.add(gc, CARD_GENERAL);

    final JFileChooser fc = FileSystemViews.get().fileChooser("replay/");

    fc.setFileFilter(new FileFilter() {
        @Override
        public String getDescription() {
            return "Zeromeaner Replay Files";
        }

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

    fc.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION))
                return;
            JFileChooser fc = (JFileChooser) e.getSource();
            String path = fc.getSelectedFile().getPath();
            if (!path.contains("replay/"))
                path = "replay/" + path;
            startReplayGame(path);
        }
    });
    fc.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentShown(ComponentEvent e) {
            fc.rescanCurrentDirectory();
        }
    });
    content.add(fc, CARD_OPEN);

    content.add(new StandaloneFeedbackPanel(), CARD_FEEDBACK);
}

From source file:os.Controller.java

public void browse() {
    FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF Files", "pdf", "PDF");
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileFilter(filter);
    fileChooser.setAcceptAllFileFilterUsed(false);
    fileChooser.setMultiSelectionEnabled(true);
    fileChooser.showOpenDialog(null);/* w  ww  . j  a v  a2s  .  c  o  m*/

    File[] files = fileChooser.getSelectedFiles();
    try {
        //        if (checkSelection(files) == true) {
        //            singleThread(files);
        multiThread(files);
    } catch (Exception ex) {
        Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
    }
    selectedTxt.append("You've selected " + files.length + " PDF file(s).\n");
    //        } else {
    //            JOptionPane.showMessageDialog(fileChooser, "Please select PDF file(s) only");
    //        }
}

From source file:paintbasico2d.VentanaPrincipal.java

private void jMenuItemGuardarImagenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemGuardarImagenActionPerformed
    VentanaInterna vi = (VentanaInterna) escritorio.getSelectedFrame();
    BufferedImage img = vi.getLienzo().getImage();
    JFileChooser dlg = new JFileChooser();
    dlg.setFileFilter(filterImgJPG);
    dlg.setFileFilter(filterImgPNG);//w  w  w .  j a v a2s  .  c om
    int resp = dlg.showSaveDialog(this);
    if (resp == JFileChooser.APPROVE_OPTION) {
        try {

            File f = dlg.getSelectedFile();
            File newFile = null;
            vi.getLienzo().volcarImg(img);
            newFile = new File(f.getAbsolutePath() + ".jpg");
            ImageIO.write(img, "jpg", newFile);
        } catch (Exception ex) {
            System.err.println("Error al guardar la imagen.");
        }
    }

}

From source file:paintbasico2d.VentanaPrincipal.java

private void jMenuItemGrabar_SonidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemGrabar_SonidoActionPerformed
    JFileChooser dlg = new JFileChooser();
    dlg.setFileFilter(filter);
    int resp = dlg.showSaveDialog(this);
    if (resp == JFileChooser.APPROVE_OPTION) {
        try {/* w  w  w.  ja  va2 s  .c om*/
            File f = dlg.getSelectedFile();
            VentanaInternaGrabacion rec = new VentanaInternaGrabacion(f);
            this.escritorio.add(rec);
            rec.setTitle(f.getName());

            rec.setVisible(true);
        } catch (Exception ex) {
            System.err.println("Error");
        }
    }
}

From source file:paintbasico2d.VentanaPrincipal.java

private void jMenuItemAbrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAbrirActionPerformed

    // TODO add your handling code here:
    JFileChooser dlg = new JFileChooser();
    dlg.setFileFilter(filter);
    dlg.setFileFilter(filterImg);//from w  ww .j ava 2s  .  c o m
    dlg.setFileFilter(filterVideo);
    dlg.setFileFilter(filterImgJPG);
    dlg.setFileFilter(filterImgPNG);
    int resp = dlg.showOpenDialog(this);
    if (resp == JFileChooser.APPROVE_OPTION) {
        try {
            File f = dlg.getSelectedFile();
            String extension = FilenameUtils.getExtension(f.getPath());

            if ("png".equals(extension) || "PNG".equals(extension) || "jpg".equals(extension)
                    || "JPG".equals(extension)) {

                BufferedImage img = ImageIO.read(f);
                VentanaInterna vi = new VentanaInterna();
                vi.getLienzo().setImage(img);

                this.escritorio.add(vi);
                vi.setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
                vi.setTitle(f.getName());
                vi.setVisible(true);
            } else if ("avi".equals(extension)) {
                VentanaInternaJMFPlayer v = new VentanaInternaJMFPlayer();
                URL url = new URL("file:" + f.getAbsolutePath());

                this.escritorio.add(v);
                v.setVisible(true);
                v.play(url);
            } else {

                VentanaInternaReproductor v = new VentanaInternaReproductor(f);
                this.escritorio.add(v);
                int x, y;
                x = (int) (Math.random() * (20 - 200) + 200);
                y = (int) (Math.random() * (20 - 200) + 200);
                v.setVisible(true);
                v.setLocation(x, y);
            }
        } catch (Exception ex) {
            System.err.println("No se ha podido abrir el fichero");
        }
    }
}