Example usage for javax.swing JFileChooser setFileSelectionMode

List of usage examples for javax.swing JFileChooser setFileSelectionMode

Introduction

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

Prototype

@BeanProperty(preferred = true, enumerationValues = { "JFileChooser.FILES_ONLY",
        "JFileChooser.DIRECTORIES_ONLY",
        "JFileChooser.FILES_AND_DIRECTORIES" }, description = "Sets the types of files that the JFileChooser can choose.")
public void setFileSelectionMode(int mode) 

Source Link

Document

Sets the JFileChooser to allow the user to just select files, just select directories, or select both files and directories.

Usage

From source file:org.parosproxy.paros.extension.beanshell.BeanShellConsoleDialog.java

private JButton getBtnSave() {
    if (btnSave == null) {
        btnSave = new JButton();
        btnSave.setText("Save...");

        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                if (currentScriptFile != null) {
                    try {
                        saveScript(getBeanShellPanel().getTxtEditor().getText(), currentScriptFile);
                        getBeanShellPanel().setSaved(true);
                    } catch (IOException ee) {
                        ee.printStackTrace();
                    }/*w ww. j  av  a 2 s .  c  o m*/

                } else {
                    JFileChooser fc = new JFileChooser(scriptsDir);
                    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
                    int result = fc.showSaveDialog(getBeanShellPanel());

                    if (result == JFileChooser.APPROVE_OPTION) {
                        try {
                            saveScript(getBeanShellPanel().getTxtEditor().getText(), fc.getSelectedFile());
                            getBeanShellPanel().setSaved(true);
                            currentScriptFile = fc.getSelectedFile();
                        } catch (IOException ee) {
                            ee.printStackTrace();
                        }
                    }
                }
            }
        });
    }
    return btnSave;
}

From source file:org.parosproxy.paros.extension.beanshell.BeanShellConsoleDialog.java

private JButton getBtnSaveAs() {
    if (btnSaveAs == null) {
        btnSaveAs = new JButton();
        btnSaveAs.setText("Save as...");

        btnSaveAs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                JFileChooser fc = new JFileChooser(scriptsDir);
                fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int result = fc.showSaveDialog(getBeanShellPanel());
                if (result == JFileChooser.APPROVE_OPTION) {
                    try {
                        saveScript(getBeanShellPanel().getTxtEditor().getText(), fc.getSelectedFile());
                        getBeanShellPanel().setSaved(true);
                        currentScriptFile = fc.getSelectedFile();
                    } catch (IOException ee) {
                        ee.printStackTrace();
                    }//from  ww  w.j ava 2 s. com
                }

            }
        });
    }
    return btnSaveAs;
}

From source file:org.p_vcd.ui.VcdDialog.java

private void selectFileButton() {
    JFileChooser fc = new JFileChooser();
    fc.setMultiSelectionEnabled(false);//ww w . j  a  va  2  s  . c  o m
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fc.setFileFilter(
            new FileNameExtensionFilter("images and videos", Parameters.get().getImageAndVideoExtensions()));
    File def = Parameters.get().getDefaultDir_QuerySelection();
    if (def != null)
        fc.setCurrentDirectory(def);
    int returnVal = fc.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        Parameters.get().updateDefaultDir_QuerySelection(fc.getCurrentDirectory());
        queryFile = fc.getSelectedFile();
        lbl_queryFile.setText(queryFile.getAbsolutePath());
        radio_queryFile.setSelected(true);
    }
}

From source file:org.richie.codeGen.ui.CodeGenMainUI.java

/**
 * /*  ww  w . ja  va  2  s . co m*/
 */
private void openPdmFile() {
    JFileChooser jfc = new JFileChooser();// 
    LastOperateVo lastOperateVo = null;
    try {
        lastOperateVo = GlobalData.getLastOperateVo();
    } catch (Exception e2) {
        log.error("??", e2);
    }
    if (lastOperateVo != null && lastOperateVo.getPdmFilePath() != null) {
        jfc.setCurrentDirectory(new File(lastOperateVo.getPdmFilePath()));// ?d
    } else {
        jfc.setCurrentDirectory(new File("d:\\"));// ?d

    }
    jfc.setFileSelectionMode(0);// ?
    jfc.setFileFilter(new FileFilter() {

        public boolean accept(File f) {
            if (f.getName().endsWith(".pdm") || f.isDirectory()) {
                return true;
            }
            return false;
        }

        public String getDescription() {
            return "pdm(*.pdm)";
        }
    });
    int state = jfc.showOpenDialog(null);
    if (state == 1) {
        return;
    } else {
        File f = jfc.getSelectedFile();
        initTree(f.getAbsolutePath());
        try {
            if (lastOperateVo == null) {
                lastOperateVo = new LastOperateVo();
            }
            lastOperateVo.setPdmFilePath(f.getAbsolutePath());
            GlobalData.saveLastOperateVo();
        } catch (Exception e1) {
            log.error("???pdm", e1);
        }
    }
}

From source file:org.richie.codeGen.ui.configui.ConstantConfigWin.java

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == addLineBtn) {
        ConstantConfigVo vo = new ConstantConfigVo();
        constantConfigModel.addRow(vo);//from  w w  w.  java2 s . co m
        table.updateUI();
    } else if (e.getSource() == saveBtn) {
        onSave();
    } else if (e.getSource() == chooseBtn1 || e.getSource() == chooseBtn2 || e.getSource() == chooseBtn3
            || e.getSource() == chooseBtn4) {
        JFileChooser jfc = new JFileChooser();// 
        if (e.getSource() == chooseBtn1) {
            jfc.setCurrentDirectory(new File(rootPathValue1.getText()));
        } else if (e.getSource() == chooseBtn2) {
            jfc.setCurrentDirectory(new File(rootPathValue2.getText()));
        } else if (e.getSource() == chooseBtn3) {
            jfc.setCurrentDirectory(new File(rootPathValue3.getText()));
        } else if (e.getSource() == chooseBtn4) {
            jfc.setCurrentDirectory(new File(rootPathValue4.getText()));
        }
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);// ?
        int status = jfc.showOpenDialog(this);
        if (status == JFileChooser.CANCEL_OPTION) {
            return;
        } else {
            File file = jfc.getSelectedFile();
            if (e.getSource() == chooseBtn1) {
                rootPathValue1.setText(file.getAbsolutePath());
            } else if (e.getSource() == chooseBtn2) {
                rootPathValue2.setText(file.getAbsolutePath());
            } else if (e.getSource() == chooseBtn3) {
                rootPathValue3.setText(file.getAbsolutePath());
            } else if (e.getSource() == chooseBtn4) {
                rootPathValue4.setText(file.getAbsolutePath());
            }
        }
    }

}

From source file:org.scify.talkandplay.gui.configuration.EntertainmentTab.java

private void setListeners() {

    musicPathTextField.addMouseListener(new MouseAdapter() {
        @Override//from  ww w. j av  a2 s .c om
        public void mouseClicked(MouseEvent me) {
            JFileChooser chooser = new JFileChooser();

            chooser.setDialogTitle(" ");
            chooser.setAcceptAllFileFilterUsed(false);
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                musicPathTextField.setText(chooser.getSelectedFile().getAbsolutePath());
            }
        }
    });

    videoPathTextField.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent me) {
            JFileChooser chooser = new JFileChooser();

            chooser.setDialogTitle(" ");
            chooser.setAcceptAllFileFilterUsed(false);
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                videoPathTextField.setText(chooser.getSelectedFile().getAbsolutePath());
            }
        }
    });

    musicPathTextField.addFocusListener(new FocusAdapter() {
        public void focusGained(FocusEvent fe) {
            if (" ".equals(musicPathTextField.getText())) {
                musicPathTextField.setText("");
            }
        }

        public void focusLost(FocusEvent fe) {
            if (musicPathTextField.getText().isEmpty()) {
                musicPathTextField.setText(" ");
            } else if (!musicPathTextField.getText().endsWith("/")) {
                musicPathTextField.setText(musicPathTextField.getText() + "/");
            }
        }
    });

    videoPathTextField.addFocusListener(new FocusAdapter() {
        public void focusGained(FocusEvent fe) {
            if (" video".equals(videoPathTextField.getText())) {
                videoPathTextField.setText("");
            }
        }

        public void focusLost(FocusEvent fe) {
            if (videoPathTextField.getText().isEmpty()) {
                videoPathTextField.setText(" video");
            } else if (!videoPathTextField.getText().endsWith("/")) {
                videoPathTextField.setText(videoPathTextField.getText() + "/");
            }
        }
    });

    songSumTextField.addFocusListener(new FocusAdapter() {
        public void focusGained(FocusEvent fe) {
            if ("".equals(songSumTextField.getText())) {
                songSumTextField.setText("");
            }
        }

        public void focusLost(FocusEvent fe) {
            if (songSumTextField.getText().isEmpty()) {
                songSumTextField.setText("");
            }
        }
    });

    backButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent me) {
            parent.goBack();
        }
    });
}

From source file:org.scify.talkandplay.gui.users.UserFormPanel.java

private void uploadImageLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_uploadImageLabelMouseClicked
    userImage = "";
    JFileChooser chooser = new JFileChooser();

    chooser.setDialogTitle(" ");
    chooser.setAcceptAllFileFilterUsed(false);
    chooser.setFileFilter(//from   ww  w. j  a  va 2  s.com
            new FileNameExtensionFilter("Image Files", "png", "jpg", "jpeg", "JPG", "JPEG", "gif"));
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        userImage = chooser.getSelectedFile().getAbsolutePath();
        uploadImageLabel.setIcon(guiHelper.getIcon(userImage));
    }
}

From source file:org.scify.talkandplay.gui.users.UserFormPanel.java

private void saveToFileButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_saveToFileButtonMouseClicked
    final JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle(" ");
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        UserService us = new UserService();
        //returns boolean to show success/failure of operation
        boolean result = us.storeUserToExternalFile(user.getName(), file.getAbsolutePath());
        //on success print success message, else print error message
        if (result) {
            errorLabel.setVisible(false);
            successLabel.setText(" ? ?.");
            successLabel.setVisible(true);
        } else {/*w  w  w  . ja v  a  2  s. com*/
            successLabel.setVisible(false);
            errorLabel.setText(
                    " ?  ?. ?  .");
            errorLabel.setVisible(true);
        }
    }
}

From source file:org.signserver.admin.gui.AddWorkerDialog.java

private void filePathBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filePathBrowseButtonActionPerformed
    final JFileChooser chooser = new JFileChooser();
    final File baseDir = SignServerAdminGUIApplication.getBaseDir();
    final String basedirPath = baseDir.getAbsolutePath();
    final File sampleDir = new File(basedirPath + File.separator + "doc" + File.separator + "sample-configs");

    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setCurrentDirectory(sampleDir.isDirectory() ? sampleDir : baseDir);
    chooser.setFileFilter(new FileNameExtensionFilter("Properties files", "properties"));

    final int res = chooser.showOpenDialog(this);

    if (res == JFileChooser.APPROVE_OPTION) {
        final File file = chooser.getSelectedFile();
        filePathTextField.setText(file.getAbsolutePath());
        fileSelected = true;//ww w .  j a  va  2 s. co m
    }

    updateControls();
}

From source file:org.signserver.admin.gui.ViewCertificateFrame.java

private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportButtonActionPerformed
    if (certificate != null) {
        final JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        final FileFilter pemFilter = new FileNameExtensionFilter("X.509 Certificate (PEM)", "pem");
        final FileFilter pemChainFilter = new FileNameExtensionFilter("X.509 Certificate with chain (PEM)",
                "pem");
        final FileFilter derFilter = new FileNameExtensionFilter("X.509 Certificate (DER)", "cer", "crt",
                "der");
        chooser.addChoosableFileFilter(pemFilter);
        chooser.addChoosableFileFilter(derFilter);
        chooser.addChoosableFileFilter(pemChainFilter);
        chooser.setDialogTitle("Save certificate as file");

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

            try {
                if (chooser.getFileFilter() == derFilter) {
                    // DER file
                    FileUtils.writeByteArrayToFile(file, certificate.getEncoded());
                } else {
                    // PEM file
                    BufferedOutputStream out = null;
                    try {
                        out = new BufferedOutputStream(new FileOutputStream(file));
                        PEMWriter pemWriter = new PEMWriter(new OutputStreamWriter(out));
                        if (chooser.getFileFilter() == pemFilter) {
                            pemWriter.writeObject(certificate);
                        } else {
                            for (X509Certificate cert : certificates) {
                                pemWriter.writeObject(cert);
                            }/* ww w  .ja v  a 2 s. c o m*/
                        }
                        pemWriter.close();
                    } finally {
                        IOUtils.closeQuietly(out);
                    }
                }
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this,
                        "Exporting certificate failed:\n" + ex.getLocalizedMessage(), "Export",
                        JOptionPane.ERROR_MESSAGE);
            } catch (CertificateEncodingException ex) {
                JOptionPane.showMessageDialog(this,
                        "Unable to encode certificate:\n" + ex.getLocalizedMessage(), "Export",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}