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.mirth.connect.client.ui.panels.export.MessageExportPanel.java

private void browseSelected() {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    if (userPreferences != null) {
        File currentDir = new File(userPreferences.get("currentDirectory", ""));

        if (currentDir.exists()) {
            chooser.setCurrentDirectory(currentDir);
        }/*from ww  w  . ja  v  a2  s . c  om*/
    }

    if (chooser.showOpenDialog(getParent()) == JFileChooser.APPROVE_OPTION) {
        if (userPreferences != null) {
            userPreferences.put("currentDirectory", chooser.getCurrentDirectory().getPath());
        }

        rootPathTextField.setText(chooser.getSelectedFile().getAbsolutePath());
    }
}

From source file:GUI.MyCustomFilter.java

private void NewFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NewFileActionPerformed
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new java.io.File("."));
    chooser.setDialogTitle("choosertitle");
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setAcceptAllFileFilterUsed(false);

    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        String path = chooser.getSelectedFile().getAbsolutePath();
        String ext = FilenameUtils.getExtension(path);
        outputPath = path;/*w  ww .  java  2s.co m*/
        projectSelected = 1;
        //System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
        //System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
    } else {
        System.out.println("No Selection ");
    }
}

From source file:be.ugent.maf.cellmissy.gui.controller.analysis.doseresponse.area.AreaDoseResponseController.java

/**
 * Ask user to choose for a directory and invoke swing worker for creating
 * PDF report//w  ww. j  a  va 2 s . co m
 *
 * @throws IOException
 */
protected void createPdfReport() throws IOException {
    Experiment experiment = areaMainController.getExperiment();
    // 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("Dose Response Report " + experiment.toString() + " - "
            + experiment.getProject().toString() + ".pdf"));
    // in response to the button click, show open dialog
    int returnVal = chooseDirectory.showSaveDialog(areaMainController.getDataAnalysisPanel());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File directory = chooseDirectory.getCurrentDirectory();
        DoseResponseReportSwingWorker doseResponseReportSwingWorker = new DoseResponseReportSwingWorker(
                directory, chooseDirectory.getSelectedFile().getName());
        doseResponseReportSwingWorker.execute();
    } else {
        areaMainController.showMessage("Open command cancelled by user", "", JOptionPane.INFORMATION_MESSAGE);
    }
}

From source file:br.upe.ecomp.dosa.view.wizard.WizardAction.java

@Override
protected void fileBrowseButtonActionPerformed(ActionEvent evt) {
    JFileChooser fileopen = new JFileChooser();
    fileopen.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    int ret = fileopen.showDialog(this, "Open file");

    if (ret == JFileChooser.APPROVE_OPTION) {
        fileLocationTextField.setText(fileopen.getSelectedFile().toString());
    }// www .  j a  v a2s .c  o m
}

From source file:br.upe.ecomp.dosa.view.mainwindow.MainWindowActions.java

private boolean chooseDirectory(JTextField textField) {
    JFileChooser fileOpen = new JFileChooser();
    fileOpen.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fileOpen.showOpenDialog(this);

    boolean directoryChoosed = false;

    if (fileOpen.getSelectedFile() != null) {
        File directory = new File(fileOpen.getSelectedFile().getAbsolutePath());
        textField.setText(directory.getPath());

        resultFiles = getFilesOnDirectory(directory);
        directoryChoosed = true;//from w  w  w  .  j  ava2 s  .  c o  m
    }

    // Codigo para Mac OSX
    // FileDialog fileopen = new FileDialog(this, "Open Results Directory", FileDialog.LOAD);
    // fileopen.setModalityType(ModalityType.DOCUMENT_MODAL);
    //
    // System.setProperty("apple.awt.fileDialogForDirectories", "true");
    // fileopen.setVisible(true);
    // System.setProperty("apple.awt.fileDialogForDirectories", "false");
    //
    // if (fileopen.getFile() != null) {
    // File directory = new File(fileopen.getDirectory(), fileopen.getFile());
    // textField.setText(directory.getPath());
    //
    // resultFiles = getFilesOnDirectory(directory);
    // // List<File> foundFiles = ;
    // // for (File file : foundFiles) {
    // // if (file.getName().endsWith(".txt")) {
    // // resultFiles.add(file);
    // // }
    // // }
    // directoryChoosed = true;
    // }
    return directoryChoosed;
}

From source file:dylemator.UserResultList.java

private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportButtonActionPerformed
    if (this.filenameCombo.getSelectedIndex() == 0)
        return;/*w w w  .ja  va2 s . c o m*/
    String sheetName = (String) this.filenameCombo.getSelectedItem();
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet(sheetName);
    Row headerRow = sheet.createRow(0);
    String[] headers = exportData.get(0);
    int numOfColumns = headers.length;
    for (int i = 0, j = 0; i < numOfColumns; i++) {
        if (i == 1 || i == 2 || i == 3) // opuszcz. date, imie, nazwisko
            continue;
        Cell cell = headerRow.createCell(j++);
        cell.setCellValue(headers[i]);
    }

    int rowCount = exportData.size();
    for (int rownum = 1; rownum < rowCount; rownum++) {
        Row row = sheet.createRow(rownum);
        String[] values = exportData.get(rownum);
        for (int i = 0, j = 0; i < numOfColumns; i++) {
            if (i == 1 || i == 2 || i == 3) // opuszcz. date, imie, nazwisko
                continue;
            Cell cell = row.createCell(j++);
            cell.setCellValue(values[i]);
        }
    }

    String defaultFilename = "Export.xlsx";
    JFileChooser f = new JFileChooser(System.getProperty("user.dir"));
    f.setSelectedFile(new File(defaultFilename));
    f.setDialogTitle("Wybierz nazw dla pliku eksportu");
    f.setFileSelectionMode(JFileChooser.FILES_ONLY);
    FileFilter ff = new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.getName().endsWith(".xlsx"))
                return true;
            return false;
        }

        @Override
        public String getDescription() {
            return "";
        }
    };
    f.setFileFilter(ff);

    File file = null;
    int save = f.showSaveDialog(this);
    if (save == JFileChooser.APPROVE_OPTION)
        file = f.getSelectedFile();
    else
        return;

    FileOutputStream out;
    try {
        out = new FileOutputStream(file);
        wb.write(out);
        out.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(UserResultList.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(UserResultList.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:baocaoxla.compare.java

private void btOutputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOutputActionPerformed
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fc.showOpenDialog(this);
    File dir = fc.getSelectedFile();
    txtOutput.setText(dir.toString());/*from www  .  j a v  a  2s . c o m*/
}

From source file:net.pandoragames.far.ui.swing.menu.FileMenu.java

private void init(final Localizer localizer, final ComponentRepository componentRepository) {
    //   Import//from  w  w w  . j  a v  a2s  . c o m
    JMenuItem importMenu = new JMenuItem(localizer.localize("label.import"));
    importMenu.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser(config.getFileListExportDirectory());
            fileChooser.setDialogTitle(localizer.localize("label.select-import-file"));
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int returnVal = fileChooser.showOpenDialog(FileMenu.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                Runnable fileImporter = new ImportAction(fileChooser.getSelectedFile(), localizer,
                        componentRepository.getOperationCallBackListener(), config.isWindows());
                Thread thread = new Thread(fileImporter);
                thread.setDaemon(true);
                thread.start();
            }
        }
    });
    this.add(importMenu);
    //   Export
    export = new JMenuItem(localizer.localize("label.export"));
    export.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ExportFileListDialog dialog = new ExportFileListDialog(mainFrame, tableModel, config);
            dialog.pack();
            dialog.setVisible(true);
        }
    });
    this.add(export);
    // seperator 
    this.addSeparator();
    //   Edit
    edit = new JMenuItem(localizer.localize("label.edit"));
    edit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FileEditor editor = new FileEditor(mainFrame, tableModel.getSelectedRows().get(0), config);
            editor.pack();
            editor.setVisible(true);
        }
    });
    this.add(edit);
    //   View
    JMenuItem view = new JMenuItem(viewAction);
    this.add(view);
    //   Preview
    JMenuItem preview = new JMenuItem(previewAction);
    this.add(preview);
    //   Info
    info = new JMenuItem(localizer.localize("label.info"));
    info.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            InfoView infoView = new InfoView(mainFrame, tableModel.getSelectedRows().get(0), config,
                    repository);
            infoView.pack();
            infoView.setVisible(true);
        }
    });
    this.add(info);
    // seperator 
    this.addSeparator();
    // copy
    copy = new JMenuItem(localizer.localize("menu.copy"));
    copy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FileOperationDialog.copyDialog(tableModel, findForm, errorSink, config, mainFrame);
        }
    });
    this.add(copy);
    // tree copy
    treeCopy = new JMenuItem(localizer.localize("menu.treeCopy"));
    treeCopy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FileOperationDialog.treeCopyDialog(tableModel, findForm, errorSink, config, mainFrame);
        }
    });
    this.add(treeCopy);
    // move
    move = new JMenuItem(localizer.localize("menu.move"));
    move.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FileOperationDialog.moveDialog(tableModel, findForm, errorSink, config, mainFrame);
        }
    });
    this.add(move);
    // delete
    delete = new JMenuItem(localizer.localize("menu.delete"));
    delete.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FileOperationDialog.deleteDialog(tableModel, findForm.getBaseDirectory(), errorSink, config,
                    mainFrame);
        }
    });
    this.add(delete);
    // rename
    rename = new JMenuItem(localizer.localize("menu.rename-dialog"));
    rename.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int rowIndex = tableModel.getRowIndex(tableModel.getSelectedRows().get(0));
            FileOperationDialog.renameDialog(rowIndex, tableModel, findForm, errorSink, config, mainFrame);
        }
    });
    this.add(rename);
}

From source file:baocaoxla.compare.java

private void btInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btInputActionPerformed
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fc.showOpenDialog(this);
    File dir = fc.getSelectedFile();
    txtInput.setText(dir.toString());//from  w ww.j  av  a 2  s . c  o  m
    /* File[] file=dir.listFiles(new FilenameFilter() {
            
     @Override
     public boolean accept(File dir, String name) {
     return name.toLowerCase().endsWith(".jpg"); //To change body of generated methods, choose Tools | Templates.
     }
     });*/
    //File[] file = dir.listFiles();
    //for (int i = 0; i < file.length; i++) {
    //  System.out.println(file[i].getName());
    //}

}