Java Utililty Methods JFileChooser

List of utility methods to do JFileChooser

Description

The list of methods to do JFileChooser are organized into topic(s).

Method

FilecorrectSelectedFileExtension(JFileChooser chooser)
correct Selected File Extension
File file = chooser.getSelectedFile();
if (file == null) {
    return null;
String name = file.getName();
String newName = name;
if (name.matches("\"[^\"]+\"")) {
    newName = name.substring(1, name.length() - 1);
...
JFileChoosercreateChooser(String lastPath, final boolean checkOverrideFile)
create Chooser
return new JFileChooser(lastPath) {
    private static final long serialVersionUID = 1L;
    @Override
    public void approveSelection() {
        File f = getSelectedFile();
        if (f.exists() && checkOverrideFile) {
            int result = JOptionPane.showConfirmDialog(this,
                    "\"" + f.getName() + "\" already exists. Do you want to overwrite it?", "Existing file",
...
JFileChoosercreateFileChooser(File presetFile, final String... postfixes)
create File Chooser
JFileChooser fc = new JFileChooser();
if (presetFile != null)
    fc.setSelectedFile(presetFile);
fc.setAcceptAllFileFilterUsed(postfixes == null || postfixes.length == 0);
if (postfixes == null)
    return fc;
for (final String postfix : postfixes) {
    if (postfix == null)
...
JFileChoosercreateFileChooser(String initDir)
create File Chooser
JFileChooser fileChooser = new JFileChooser(new File(initDir));
fileChooser.setFileFilter(new FileFilter() {
    @Override
    public boolean accept(File f) {
        return f.getName().endsWith(".adv.xml") || f.isDirectory();
    @Override
    public String getDescription() {
...
JFileChoosercreateFileChooser(String title, File dir, String filter)
create File Chooser
return createFileChooser(title, dir, new String[] { filter });
JFileChoosercreateJFileChooser(String name)
Returns a new JFileChooser properly set up for Tetrad.
if (name == null) {
    name = "Save";
JFileChooser chooser = new JFileChooser();
String sessionSaveLocation = Preferences.userRoot().get("fileSaveLocation", "");
chooser.setCurrentDirectory(new File(sessionSaveLocation));
chooser.resetChoosableFileFilters();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
...
JFileChoosercreateJFileChooserWithExistenceChecking()
create J File Chooser With Existence Checking
return new JFileChooser() {
    public void approveSelection() {
        File[] files = selectedFiles(this);
        if (files.length == 0) {
            return;
        for (int i = 0; i < files.length; i++) {
            if (!files[i].exists() && !files[i].isFile()) {
...
JFileChoosercreateJFileChooserWithOverwritePrompting()
create J File Chooser With Overwrite Prompting
return new JFileChooser() {
    public void approveSelection() {
        if (selectedFiles(this).length != 1) {
            return;
        File selectedFile = selectedFiles(this)[0];
        if (selectedFile.exists() && !selectedFile.isFile()) {
            return;
...
FilecreateOpenFileChooser(String title, String dir, Component parent, FileFilter filter)
Create and open a single file chooser.
final JFileChooser fileChooser = new JFileChooser(dir);
fileChooser.setDialogTitle(title);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setFileFilter(filter);
final int approve = fileChooser.showOpenDialog(parent);
if (approve == JFileChooser.APPROVE_OPTION) {
...
JFileChoosercreateReportFileChooser(String curDir, File defaultReportFile)
create Report File Chooser
JFileChooser fc = new JFileChooser(curDir);
fc.setFileFilter(new FileFilter() {
    @Override
    public boolean accept(File f) {
        String filename = f.getName();
        if (filename.endsWith(".html") || filename.endsWith(".htm") || filename.endsWith(".xhtml"))
            return true;
        return false;
...