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:com.claim.ui.UiReportType5.java

private void jButtonFolder3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFolder3ActionPerformed
    try {//from w  w  w. ja  va2 s . c o  m
        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle("Open Folder");
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fc.setCurrentDirectory(new File(""));
        int result = fc.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File a = fc.getSelectedFile();
            // Path File method in this class
            String temp = a.getPath();
            txtPathFileType5.setText(".");

            if (temp != null) {
                txtPathFileType5.setText(temp);
                //txtPathFileOpaeIndv.setEnabled(true);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.sf.jabref.openoffice.AutoDetectPaths.java

private boolean autoDetectPaths() {

    if (OS.WINDOWS) {
        List<File> progFiles = findProgramFilesDir();
        File sOffice = null;/*from   w w w .  ja v  a 2 s . c  o m*/
        List<File> sofficeFiles = new ArrayList<>();
        for (File dir : progFiles) {
            if (fileSearchCancelled) {
                return false;
            }
            sOffice = findFileDir(dir, "soffice.exe");
            if (sOffice != null) {
                sofficeFiles.add(sOffice);
            }
        }
        if (sOffice == null) {
            JOptionPane.showMessageDialog(parent, Localization.lang(
                    "Unable to autodetect OpenOffice/LibreOffice installation. Please choose the installation directory manually."),
                    Localization.lang("Could not find OpenOffice/LibreOffice installation"),
                    JOptionPane.INFORMATION_MESSAGE);
            JFileChooser jfc = new JFileChooser(new File("C:\\"));
            jfc.setDialogType(JFileChooser.OPEN_DIALOG);
            jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {

                @Override
                public boolean accept(File file) {
                    return file.isDirectory();
                }

                @Override
                public String getDescription() {
                    return Localization.lang("Directories");
                }
            });
            jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            jfc.showOpenDialog(parent);
            if (jfc.getSelectedFile() != null) {
                sOffice = jfc.getSelectedFile();
            }
        }
        if (sOffice == null) {
            return false;
        }

        if (sofficeFiles.size() > 1) {
            // More than one file found
            DefaultListModel<File> mod = new DefaultListModel<>();
            for (File tmpfile : sofficeFiles) {
                mod.addElement(tmpfile);
            }
            JList<File> fileList = new JList<>(mod);
            fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            fileList.setSelectedIndex(0);
            FormBuilder b = FormBuilder.create()
                    .layout(new FormLayout("left:pref", "pref, 2dlu, pref, 4dlu, pref"));
            b.add(Localization.lang("Found more than one OpenOffice/LibreOffice executable.")).xy(1, 1);
            b.add(Localization.lang("Please choose which one to connect to:")).xy(1, 3);
            b.add(fileList).xy(1, 5);
            int answer = JOptionPane.showConfirmDialog(null, b.getPanel(),
                    Localization.lang("Choose OpenOffice/LibreOffice executable"),
                    JOptionPane.OK_CANCEL_OPTION);
            if (answer == JOptionPane.CANCEL_OPTION) {
                return false;
            } else {
                sOffice = fileList.getSelectedValue();
            }

        } else {
            sOffice = sofficeFiles.get(0);
        }
        return setupPreferencesForOO(sOffice.getParentFile(), sOffice, "soffice.exe");
    } else if (OS.OS_X) {
        File rootDir = new File("/Applications");
        File[] files = rootDir.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory() && ("OpenOffice.org.app".equals(file.getName())
                        || "LibreOffice.app".equals(file.getName()))) {
                    rootDir = file;
                    break;
                }
            }
        }
        File sOffice = findFileDir(rootDir, SOFFICE_BIN);
        if (fileSearchCancelled) {
            return false;
        }
        if (sOffice == null) {
            return false;
        } else {
            return setupPreferencesForOO(rootDir, sOffice, SOFFICE_BIN);
        }
    } else {
        // Linux:
        String usrRoot = "/usr/lib";
        File inUsr = findFileDir(new File(usrRoot), SOFFICE);
        if (fileSearchCancelled) {
            return false;
        }
        if (inUsr == null) {
            inUsr = findFileDir(new File("/usr/lib64"), SOFFICE);
            if (inUsr != null) {
                usrRoot = "/usr/lib64";
            }
        }

        if (fileSearchCancelled) {
            return false;
        }
        File inOpt = findFileDir(new File("/opt"), SOFFICE);
        if (fileSearchCancelled) {
            return false;
        }
        if ((inUsr != null) && (inOpt == null)) {
            return setupPreferencesForOO(usrRoot, inUsr, SOFFICE_BIN);
        } else if (inOpt != null) {
            if (inUsr == null) {
                return setupPreferencesForOO("/opt", inOpt, SOFFICE_BIN);
            } else { // Found both
                JRadioButton optRB = new JRadioButton(inOpt.getPath(), true);
                JRadioButton usrRB = new JRadioButton(inUsr.getPath(), false);
                ButtonGroup bg = new ButtonGroup();
                bg.add(optRB);
                bg.add(usrRB);
                FormBuilder b = FormBuilder.create()
                        .layout(new FormLayout("left:pref", "pref, 2dlu, pref, 2dlu, pref "));
                b.add(Localization.lang(
                        "Found more than one OpenOffice/LibreOffice executable. Please choose which one to connect to:"))
                        .xy(1, 1);
                b.add(optRB).xy(1, 3);
                b.add(usrRB).xy(1, 5);
                int answer = JOptionPane.showConfirmDialog(null, b.getPanel(),
                        Localization.lang("Choose OpenOffice/LibreOffice executable"),
                        JOptionPane.OK_CANCEL_OPTION);
                if (answer == JOptionPane.CANCEL_OPTION) {
                    return false;
                }
                if (optRB.isSelected()) {
                    return setupPreferencesForOO("/opt", inOpt, SOFFICE_BIN);
                } else {
                    return setupPreferencesForOO(usrRoot, inUsr, SOFFICE_BIN);
                }
            }
        }
    }
    return false;
}

From source file:jatoo.app.App.java

private File select(final File currentDirectory, final int fileSelectionMode) {

    JFileChooser fileChooser = new JFileChooser(currentDirectory);
    fileChooser.setFileSelectionMode(fileSelectionMode);

    int returnValue = fileChooser.showOpenDialog(window);

    if (returnValue == JFileChooser.APPROVE_OPTION) {
        return fileChooser.getSelectedFile();
    }/* w  w  w. java  2s.c  o m*/

    return null;
}

From source file:com.mgmtp.jfunk.core.scripting.ScriptContext.java

/**
 * Opens a file chooser dialog which can then be used to choose a file or directory and assign
 * the path of the chosen object to a variable. The name of the variable must be passed as a
 * parameter.//from   ww  w  .jav  a  2s .c  o m
 * 
 * @param fileKey
 *            the key the selected file path is stored under in the configuration
 * @return the chosen file
 */
@Cmd
public File chooseFile(final String fileKey) {
    log.debug("Opening file chooser dialog");
    JFileChooser fileChooser = new JFileChooser(System.getProperty(JFunkConstants.USER_DIR));
    fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fileChooser.setPreferredSize(new Dimension(600, 326));
    int fileChooserResult = fileChooser.showOpenDialog(null);

    if (fileChooserResult == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();
        String filename = file.toString();
        log.info("Assigning file path '{}' to property '{}'", filename, fileKey);
        config.put(fileKey, filename);
        return file;
    }

    log.error("No file or directory was chosen, execution will abort");
    throw new IllegalArgumentException("No file or directory was chosen");
}

From source file:com.igormaznitsa.sciareto.ui.MainFrame.java

private void menuNewProjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuNewProjectActionPerformed
    final JFileChooser folder = new JFileChooser();
    folder.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    folder.setDialogTitle("Create project folder");
    folder.setApproveButtonText("Create");
    folder.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    if (folder.showOpenDialog(Main.getApplicationFrame()) == JFileChooser.APPROVE_OPTION) {
        final File file = folder.getSelectedFile();
        if (file.exists()) {
            DialogProviderManager.getInstance().getDialogProvider()
                    .msgError("File '" + file.getName() + "' already exists!");
        } else if (file.mkdirs()) {
            if (PreferencesManager.getInstance().getPreferences()
                    .getBoolean(PreferencesPanel.PREFERENCE_KEY_KNOWLEDGEFOLDER_ALLOWED, true)) {
                final File knowledgeFolder = new File(file, ".projectKnowledge");
                knowledgeFolder.mkdirs();
            }/*from   w  w  w  .j a  v a  2  s .  co m*/
            if (openProject(file, true)) {
                this.focusInTree(file);
            }
        } else {
            LOGGER.error("Can't create folder : " + file);
            DialogProviderManager.getInstance().getDialogProvider().msgError("Can't create folder: " + file);
        }
    }
}

From source file:be.ugent.maf.cellmissy.gui.controller.analysis.singlecell.AngleDirectController.java

/**
 *
 * @throws IOException//from w  w  w  .  j av a  2  s  . com
 */
private void createPdf(JFreeChart chart) throws IOException {
    // choose directory to save pdf file
    JFileChooser chooseDirectory = new JFileChooser();
    chooseDirectory.setDialogTitle("Choose a directory to save the report");
    chooseDirectory.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooseDirectory.setSelectedFile(new File("chart rose plot" + ".pdf"));
    // in response to the button click, show open dialog
    int returnVal = chooseDirectory.showSaveDialog(angleDirectPanel);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File directory = chooseDirectory.getCurrentDirectory();
        PdfSwingWorker pdfSwingWorker = new PdfSwingWorker(directory,
                chooseDirectory.getSelectedFile().getName(), chart);
        pdfSwingWorker.execute();
    } else {
        singleCellPreProcessingController.showMessage("Open command cancelled by user", "",
                JOptionPane.INFORMATION_MESSAGE);
    }
}

From source file:com.jvms.i18neditor.editor.Editor.java

public void showCreateProjectDialog(ResourceType type) {
    JFileChooser fc = new JFileChooser();
    fc.setDialogTitle(MessageBundle.get("dialogs.project.new.title"));
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int result = fc.showOpenDialog(this);
    if (result == JFileChooser.APPROVE_OPTION) {
        createProject(Paths.get(fc.getSelectedFile().getPath()), type);
    } else {//  w w w .  j  a  va2 s  . co  m
        updateHistory();
        updateUI();
    }
}

From source file:com.jvms.i18neditor.editor.Editor.java

public void showImportProjectDialog() {
    String path = null;//www .  j av a 2 s  .  com
    if (project != null) {
        path = project.getPath().toString();
    }
    JFileChooser fc = new JFileChooser(path);
    fc.setDialogTitle(MessageBundle.get("dialogs.project.import.title"));
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int result = fc.showOpenDialog(this);
    if (result == JFileChooser.APPROVE_OPTION) {
        importProject(Paths.get(fc.getSelectedFile().getPath()), true);
    }
}

From source file:io.heming.accountbook.ui.MainFrame.java

private void exportRecords() {
    final JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.addChoosableFileFilter(new AbbFileFilter());
    chooser.setAcceptAllFileFilterUsed(false);
    int value = chooser.showSaveDialog(this);
    if (value == JFileChooser.APPROVE_OPTION) {
        final File file = chooser.getSelectedFile();
        disableAllControls();/* w  ww  .jav a 2s  .c  om*/
        new Thread() {
            @Override
            public void run() {
                try {
                    statusLabel.setIcon(new ImageIcon(getClass().getResource("loader.gif")));
                    setStatusText("?...");
                    FacadeUtil.shutdown();
                    // .abb?
                    setStatusText("?...");
                    List<String> exclusion = Arrays.asList();
                    if (!file.getName().endsWith(".abb")) {
                        ZIPUtil.compress(new File(Constants.DATA_DIR),
                                new File(file.getAbsolutePath() + ".abb"), exclusion);
                    } else {
                        ZIPUtil.compress(new File(Constants.DATA_DIR), file, exclusion);
                    }
                    setStatusText("??...");
                    FacadeUtil.restart();
                    enableAllControls();
                    setStatusText("");
                    statusLabel.setIcon(new ImageIcon(getClass().getResource("stopped-loader.png")));
                    JOptionPane.showMessageDialog(MainFrame.this, String.format("?"), "?",
                            JOptionPane.INFORMATION_MESSAGE);
                } catch (IOException e) {
                    JOptionPane.showMessageDialog(MainFrame.this, String.format(""), "",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        }.start();
    }
}

From source file:net.itransformers.topologyviewer.gui.GraphViewerPanel.java

private JButton createCaptureButton() {
    JButton capture = new JButton("Capture");
    capture.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            JFileChooser chooser = new JFileChooser(currentDir);
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            chooser.setMultiSelectionEnabled(false);
            chooser.setFileFilter(new PngFileFilter());
            int result = chooser.showSaveDialog(GraphViewerPanel.this);
            if (result == JFileChooser.APPROVE_OPTION) {
                currentDir = chooser.getCurrentDirectory();
                String absolutePath = chooser.getSelectedFile().getAbsolutePath();
                if (!absolutePath.endsWith(".png")) {
                    absolutePath += ".png";
                }//from w  w  w  . j a  v  a  2s .c  o  m
                try {
                    vv.setDoubleBuffered(false);
                    writeToImageFile(absolutePath);
                    vv.setDoubleBuffered(true);
                } catch (AWTException e1) {
                    e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
                }
                //                        captureToFile(absolutePath);
                //                        writeJPEGImage(absolutePath + ".JPEG");
                //                        captureScreen(absolutePath);
            }
        }
    });
    return capture;
}