Java JFileChooser chooseFile(File initialFile, boolean load)

Here you can find the source of chooseFile(File initialFile, boolean load)

Description

choose File

License

Open Source License

Declaration

public static File chooseFile(File initialFile, boolean load) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;

import java.io.*;
import java.awt.FileDialog;

public class Main {
    public static File chooseFile(File initialFile, boolean load) {
        if (isMac()) {
            FileDialog d = new FileDialog((java.awt.Frame) null);
            d.setMode(load ? FileDialog.LOAD : FileDialog.SAVE);
            if (initialFile != null) {
                d.setDirectory(initialFile.getParent());
                d.setFile(initialFile.getName());
            }/*from   w  w w .  j a va 2s.  c o  m*/
            d.show();
            String f = d.getFile();
            if (f != null)
                return new File(new File(d.getDirectory()), d.getFile());
        } else {
            JFileChooser chooser = new JFileChooser();
            if (initialFile != null)
                chooser.setSelectedFile(initialFile);
            if ((load ? chooser.showOpenDialog(null) : chooser.showSaveDialog(null)) == JFileChooser.APPROVE_OPTION)
                return chooser.getSelectedFile();
        }
        return null;
    }

    public static boolean isMac() {
        return System.getProperty("os.name").startsWith("Mac");
    }
}

Related

  1. chooseFile()
  2. chooseFile()
  3. chooseFile(Component p, String message, boolean toOpen, int fileType)
  4. chooseFile(Component parent, String title, boolean open)
  5. chooseFile(Component parentComponent, String title, File curDir, String suffix)
  6. chooseFile(final Component parent, final boolean filesOnly, final String title, final File selectedFile, final FileFilter filter)
  7. ChooseFile(final String desc, final String[] allowed_extensions, String suggested_dir)
  8. chooseFile(JFileChooser fc, Component parent)
  9. chooseFile(String title)