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.apache.cayenne.modeler.dialog.codegen.GeneratorController.java

/**
 * An action method that pops up a file chooser dialog to pick the
 * generation directory.//from  w  ww . j ava2 s.  com
 */
public void selectOutputFolderAction() {

    JTextField outputFolder = ((GeneratorControllerPanel) getView()).getOutputFolder();

    String currentDir = outputFolder.getText();

    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setDialogType(JFileChooser.OPEN_DIALOG);

    // guess start directory
    if (!Util.isEmptyString(currentDir)) {
        chooser.setCurrentDirectory(new File(currentDir));
    } else {
        FSPath lastDir = Application.getInstance().getFrameController().getLastDirectory();
        lastDir.updateChooser(chooser);
    }

    int result = chooser.showOpenDialog(getView());
    if (result == JFileChooser.APPROVE_OPTION) {
        File selected = chooser.getSelectedFile();

        // update model
        String path = selected.getAbsolutePath();
        outputFolder.setText(path);
        setOutputPath(path);
    }
}

From source file:org.apache.cayenne.modeler.dialog.pref.ClasspathPreferences.java

protected void chooseClassEntry(FileFilter filter, String title, int selectionMode) {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(selectionMode);
    chooser.setDialogType(JFileChooser.OPEN_DIALOG);
    chooser.setAcceptAllFileFilterUsed(true);

    getLastDirectory().updateChooser(chooser);

    if (filter != null) {
        chooser.addChoosableFileFilter(filter);
    }//from ww  w .  ja  v a2  s.c o m

    chooser.setDialogTitle(title);

    File selected = null;
    int result = chooser.showOpenDialog(view);
    if (result == JFileChooser.APPROVE_OPTION) {
        selected = chooser.getSelectedFile();
    }

    if (selected != null) {
        if (!classPathEntries.contains(selected.getAbsolutePath())) {
            // store last dir in preferences
            getLastDirectory().updateFromChooser(chooser);

            int len = classPathEntries.size();
            int key = ++counter;

            String value = selected.getAbsolutePath();
            addChangedPreferences(Integer.toString(key), value);
            classPathEntries.add(value);
            classPathKeys.add(Integer.toString(key));

            tableModel.fireTableRowsInserted(len, len);
        }
    }
}

From source file:org.apache.cayenne.modeler.graph.action.SaveAsImageAction.java

@Override
public void performAction(ActionEvent e) {
    // find start directory in preferences
    FSPath lastDir = getApplication().getFrameController().getLastDirectory();

    // configure dialog
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    lastDir.updateChooser(chooser);//from  w ww . jav  a 2  s  . c o m

    chooser.setAcceptAllFileFilterUsed(false);

    String ext = "png";
    chooser.addChoosableFileFilter(FileFilters.getExtensionFileFilter(ext, "PNG Images"));

    int status = chooser.showSaveDialog(Application.getFrame());
    if (status == JFileChooser.APPROVE_OPTION) {
        lastDir.updateFromChooser(chooser);

        String path = chooser.getSelectedFile().getPath();
        if (!path.endsWith("." + ext)) {
            path += "." + ext;
        }

        try {

            JGraph graph = dataDomainGraphTab.getGraph();
            BufferedImage img = graph.getImage(null, 0);

            try (OutputStream out = new FileOutputStream(path);) {
                ImageIO.write(img, ext, out);
                out.flush();
            }

        } catch (IOException ex) {
            logObj.error("Could not save image", ex);
            JOptionPane.showMessageDialog(Application.getFrame(), "Could not save image.", "Error saving image",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}

From source file:org.apache.oodt.cas.workflow.gui.WorkflowGUI.java

public JMenuBar generateMenuBar() {
    JMenuBar bar = new JMenuBar();
    FileMenu fileMenu = new FileMenu();
    bar.add(fileMenu);//ww  w.ja v  a 2s . c o m
    fileMenu.getExit().addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent event) {
            System.exit(1);
        }
    });

    fileMenu.getOpenWorkspace().addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                JFileChooser chooser = new JFileChooser(new File(".")) {
                    boolean acceptFile(File f) {
                        return f.isDirectory();
                    }
                };
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int value = chooser.showOpenDialog(WorkflowGUI.this);
                if (value == JFileChooser.APPROVE_OPTION) {
                    workspace = chooser.getSelectedFile();
                    updateWorkspaceText();
                    perspective.reset();
                    loadProjects();
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    });
    fileMenu.getImport().addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            try {
                if (workspace == null) {
                    return;
                }
                JFileChooser chooser = new JFileChooser(new File("."));
                int value = chooser.showOpenDialog(WorkflowGUI.this);
                if (value == JFileChooser.APPROVE_OPTION) {
                    File file = chooser.getSelectedFile();
                    XmlWorkflowModelRepositoryFactory factory = new XmlWorkflowModelRepositoryFactory();
                    factory.setWorkspace(workspace.getAbsolutePath());
                    View activeView = perspective.getActiveView();

                    if (activeView != null) {
                        // TODO: add code for import
                    }
                }
            } catch (Exception e) {
                LOG.log(Level.SEVERE, e.getMessage());
            }
        }

    });
    fileMenu.getNewWorkspace().addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            JFileChooser chooser = new JFileChooser(new File(".")) {
                boolean acceptFile(File f) {
                    return f.isDirectory();
                }
            };
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int value = chooser.showOpenDialog(WorkflowGUI.this);
            if (value == JFileChooser.APPROVE_OPTION) {
                workspace = chooser.getSelectedFile();
                updateWorkspaceText();
                perspective.reset();
                loadProjects();
                perspective.refresh();
            }
        }
    });

    fileMenu.getNewProject().addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            // TODO: add new project code
        }
    });
    fileMenu.getSave().addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                repo.save();
            } catch (Exception e) {
                LOG.log(Level.SEVERE, e.getMessage());
            }
        }
    });
    EditMenu editMenu = new EditMenu();
    bar.add(editMenu);
    editMenu.getUndo().addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                perspective.undo();
            } catch (Exception e) {
                LOG.log(Level.SEVERE, e.getMessage());
            }
        }
    });
    bar.revalidate();
    return bar;
}

From source file:org.apache.uima.tools.docanalyzer.DBAnnotationViewerDialog.java

/**
 * If the current AE filename is not know ask for it. Then parse the selected file and return the
 * AnalysisEngineDescription object.//w ww  .  j  a  va 2  s  .c  o  m
 * 
 * @return the selected AnalysisEngineDescription, null if the user cancelled
 */
protected AnalysisEngineDescription promptForAE()
        throws IOException, InvalidXMLException, ResourceInitializationException {
    if (med1.getTAEfile() != null) {
        File taeFile = new File(med1.getTAEfile());
        XMLInputSource in = new XMLInputSource(taeFile);
        AnalysisEngineDescription aed = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(in);
        return aed;
    } else {
        String taeDir = med1.getTAEfile();
        JFileChooser chooser = new JFileChooser(taeDir);
        chooser.setDialogTitle("Select the Analysis Engine that Generated this Output");
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            XMLInputSource in = new XMLInputSource(chooser.getSelectedFile());
            return UIMAFramework.getXMLParser().parseAnalysisEngineDescription(in);
        } else {
            return null;
        }
    }
}

From source file:org.domainmath.gui.MainFrame.java

public void browse() {
    JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(new File(System.getProperty("user.dir") + File.separator + "works"));
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fc.setMultiSelectionEnabled(false);/*from  ww w  .jav a 2s.co m*/

    File _file;
    int returnVal = fc.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        _file = fc.getSelectedFile();
        setDir(_file.getAbsolutePath());

    }
}

From source file:org.domainmath.gui.MainFrame.java

private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
    JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(new File(System.getProperty("user.dir") + File.separator + "works"));
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fc.setMultiSelectionEnabled(false);/*from ww w. j  a v a2  s .  com*/

    Path browse_file;
    int returnVal = fc.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        browse_file = fc.getSelectedFile().toPath();
        setDir(browse_file.toString());
        requestToChangeDir(browse_file.toString());
        fileTreePanel.updateFileTree(fc.getSelectedFile());

    }

}

From source file:org.domainmath.gui.MainFrame.java

public void saveplot() {
    JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(new File(System.getProperty("user.dir") + File.separator + "works"));
    fc.setFileFilter(DomainMathFileFilter.SAVE_PLOT_FILE_FILTER);
    fc.setAcceptAllFileFilterUsed(false);
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fc.setMultiSelectionEnabled(false);/*from  w  w w .  ja va2  s.com*/

    File file_plot;
    String name;
    int returnVal = fc.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        System.err.println(fc.getFileFilter().getDescription());
        file_plot = fc.getSelectedFile();
        name = file_plot.getName();
        evaluateWithOutput("saveas(1," + "'" + file_plot.getAbsolutePath() + "');");

    }
}

From source file:org.drugis.mtc.gui.GeneratedCodeWindow.java

private JButton createSaveButton() {
    JButton saveButton = new JButton("Save", MainWindow.IMAGELOADER.getIcon("savefile.gif"));
    saveButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                JFileChooser chooser = new JFileChooser();
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

                int returnVal = chooser.showSaveDialog(GeneratedCodeWindow.this);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = chooser.getSelectedFile();
                    if (!file.isDirectory()) {
                        JOptionPane.showMessageDialog(GeneratedCodeWindow.this,
                                "Error: please select a directory to save to", "Files could not be saved.",
                                JOptionPane.ERROR_MESSAGE);
                    }/*from  w  w w. j a  v  a  2s  . com*/
                    writeFiles(file);
                    JOptionPane.showMessageDialog(GeneratedCodeWindow.this,
                            "Your files have been saved to: \n" + file.getPath(), "Files saved.",
                            JOptionPane.INFORMATION_MESSAGE);
                }
            } catch (IOException e) {
                JOptionPane.showMessageDialog(GeneratedCodeWindow.this, "Error: " + e.getMessage(),
                        "Files could not be saved.", JOptionPane.ERROR_MESSAGE);
            }
        }
    });
    return saveButton;
}

From source file:org.esa.snap.smart.configurator.ui.PerformancePanel.java

private void browseCachePathButtonActionPerformed() {
    JFileChooser fileChooser = new JFileChooser(cachePathTextField.getText());
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int returnValue = fileChooser.showOpenDialog(this);
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        File selectedDir = fileChooser.getSelectedFile();
        cachePathTextField.setText(selectedDir.getAbsolutePath());
        cachePathTextField.setForeground(CURRENT_VALUES_COLOR);
        controller.changed();// ww w .  j  av  a 2 s .  c o m
    }
}