Example usage for javax.swing JFileChooser showOpenDialog

List of usage examples for javax.swing JFileChooser showOpenDialog

Introduction

In this page you can find the example usage for javax.swing JFileChooser showOpenDialog.

Prototype

public int showOpenDialog(Component parent) throws HeadlessException 

Source Link

Document

Pops up an "Open File" file chooser dialog.

Usage

From source file:Main.java

public static int showFileChooserLoop(Component parentComponent, JFileChooser chooser) {
    for (;;) {/*from   w  w  w  .ja  v  a2s .c o  m*/
        int option = chooser.showOpenDialog(parentComponent);
        if (option != JFileChooser.APPROVE_OPTION) {
            return JOptionPane.CANCEL_OPTION;
        }

        File file = chooser.getSelectedFile();
        if (file.exists()) {
            int op2 = showReplaceExistingFileConfirmDialog(parentComponent, file);
            if (op2 == JOptionPane.YES_OPTION) {
                return JOptionPane.YES_OPTION;
            } else if (op2 == JOptionPane.CANCEL_OPTION) {
                return JOptionPane.CANCEL_OPTION;
            }
        } else {
            return JOptionPane.YES_OPTION;
        }
    }
}

From source file:ExcelComponents.FileOpener.java

public static File openfile() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setAcceptAllFileFilterUsed(false);
    fileChooser.addChoosableFileFilter(excelfilter);

    int returnValue = fileChooser.showOpenDialog(null);
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        return (selectedFile);
    }/*w ww.j a v a  2 s  .com*/
    return null;

}

From source file:Main.java

/**
 * opens a file using a JFileChooser//from   www .j a  va2s. co  m
 *
 * @param c          parent
 * @param mode       selection Mode {@link JFileChooser}
 * @param buttonText buttonText, uses "Open" when null ({@link JFileChooser})
 * @return File
 */
@SuppressWarnings({ "SameParameterValue", "WeakerAccess" })
public static File openJFileChooser(Component c, File currentDirectory, int mode, String buttonText) {
    JFileChooser fc = new JFileChooser(currentDirectory);
    fc.setFileSelectionMode(mode);
    int result;
    if (buttonText != null) {
        result = fc.showDialog(c, buttonText);
    } else {
        result = fc.showOpenDialog(c);
    }
    if (result == JFileChooser.APPROVE_OPTION) {
        return fc.getSelectedFile();
    }
    return null;
}

From source file:Main.java

public static File chooseDir(Component parent, String title) {
    JFileChooser chooser = new JFileChooser();
    // chooser.setCurrentDirectory(new java.io.File("."));
    chooser.setDialogTitle(title);//from   www. j  a  v  a  2 s  .c om
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setApproveButtonText("Select Dir");
    return chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION ? chooser.getSelectedFile() : null;
}

From source file:net.fabricmc.installer.installer.LocalVersionInstaller.java

public static void install(File mcDir, IInstallerProgress progress) throws Exception {
    JFileChooser fc = new JFileChooser();
    fc.setDialogTitle(Translator.getString("install.client.selectCustomJar"));
    fc.setFileFilter(new FileNameExtensionFilter("Jar Files", "jar"));
    if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        File inputFile = fc.getSelectedFile();

        JarFile jarFile = new JarFile(inputFile);
        Attributes attributes = jarFile.getManifest().getMainAttributes();
        String mcVersion = attributes.getValue("MinecraftVersion");
        Optional<String> stringOptional = ClientInstaller.isValidInstallLocation(mcDir, mcVersion);
        jarFile.close();//www  . j a  v  a 2 s  . co  m
        if (stringOptional.isPresent()) {
            throw new Exception(stringOptional.get());
        }
        ClientInstaller.install(mcDir, mcVersion, progress, inputFile);
    } else {
        throw new Exception("Failed to find jar");
    }

}

From source file:net.fabricmc.installer.installer.LocalVersionInstaller.java

public static void installServer(File mcDir, IInstallerProgress progress) throws Exception {
    JFileChooser fc = new JFileChooser();
    fc.setDialogTitle(Translator.getString("install.client.selectCustomJar"));
    fc.setFileFilter(new FileNameExtensionFilter("Jar Files", "jar"));
    if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        File inputFile = fc.getSelectedFile();

        JarFile jarFile = new JarFile(inputFile);
        Attributes attributes = jarFile.getManifest().getMainAttributes();
        String fabricVersion = attributes.getValue("FabricVersion");
        jarFile.close();/*from  ww w  . java  2s .c om*/
        File fabricJar = new File(mcDir, "fabric-" + fabricVersion + ".jar");
        if (fabricJar.exists()) {
            fabricJar.delete();
        }
        FileUtils.copyFile(inputFile, fabricJar);
        ServerInstaller.install(mcDir, fabricVersion, progress, fabricJar);
    } else {
        throw new Exception("Failed to find jar");
    }

}

From source file:ExcelComponents.FileOpener.java

public static File[] openfiles() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setAcceptAllFileFilterUsed(false);
    fileChooser.addChoosableFileFilter(excelfilter);
    fileChooser.setMultiSelectionEnabled(true);
    int returnValue = fileChooser.showOpenDialog(null);
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        File[] selectedFiles = fileChooser.getSelectedFiles().clone();
        return (selectedFiles);
    }/*ww w.j  a  v  a2 s . c  om*/
    return null;

}

From source file:Main.java

/**
 * Displays a {@link JFileChooser} to select a directory.
 *
 * @param title The title of the dialog.
 * @param startDirectory The directory where the dialog is initialed opened.
 * @return The {@link File} selected, returns null if no directory was selected.
 *///from w w w .j  a va2 s .co  m
public static File chooseDirectory(String title, File startDirectory) {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setDialogTitle(title);

    if (startDirectory != null) {
        chooser.setCurrentDirectory(startDirectory);
    }

    int status = chooser.showOpenDialog(null);

    if (status == JFileChooser.APPROVE_OPTION) {
        return chooser.getSelectedFile();
    }

    return null;
}

From source file:net.rptools.maptool.util.AssetExtractor.java

public static void extract() throws Exception {
    new Thread() {
        @Override//from   www.  j a v  a  2 s . co m
        public void run() {
            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(false);
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            if (chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {
                return;
            }
            File file = chooser.getSelectedFile();
            File newDir = new File(file.getParentFile(),
                    file.getName().substring(0, file.getName().lastIndexOf('.')) + "_images");

            JLabel label = new JLabel("", JLabel.CENTER);
            JFrame frame = new JFrame();
            frame.setTitle("Campaign Image Extractor");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setSize(400, 75);
            frame.add(label);
            SwingUtil.centerOnScreen(frame);
            frame.setVisible(true);
            Reader r = null;
            OutputStream out = null;
            PackedFile pakfile = null;
            try {
                newDir.mkdirs();

                label.setText("Loading campaign ...");
                pakfile = new PackedFile(file);

                Set<String> files = pakfile.getPaths();
                XStream xstream = new XStream();
                int count = 0;
                for (String filename : files) {
                    count++;
                    if (filename.indexOf("assets") < 0) {
                        continue;
                    }
                    r = pakfile.getFileAsReader(filename);
                    Asset asset = (Asset) xstream.fromXML(r);
                    IOUtils.closeQuietly(r);

                    File newFile = new File(newDir, asset.getName() + ".jpg");
                    label.setText("Extracting image " + count + " of " + files.size() + ": " + newFile);
                    if (newFile.exists()) {
                        newFile.delete();
                    }
                    newFile.createNewFile();
                    out = new FileOutputStream(newFile);
                    FileUtil.copyWithClose(new ByteArrayInputStream(asset.getImage()), out);
                }
                label.setText("Done.");
            } catch (Exception ioe) {
                MapTool.showInformation("AssetExtractor failure", ioe);
            } finally {
                if (pakfile != null)
                    pakfile.close();
                IOUtils.closeQuietly(r);
                IOUtils.closeQuietly(out);
            }
        }
    }.start();
}

From source file:br.univali.ps.fuzzy.portugolFuzzyCorretor.control.FileController.java

public static String getCodigoPortugol() throws FileNotFoundException {
    JFileChooser fc = new JFileChooser(currentDirectory);
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Portugol Code", "por", "txt");
    fc.setFileFilter(filter);//from w w  w . j a  va  2 s .c om
    fc.setAcceptAllFileFilterUsed(false);
    File file = null;
    int returnVal = fc.showOpenDialog(null);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        file = fc.getSelectedFile();
        currentDirectory = file.getParentFile().getPath();
        return formatarArquivoTexto(file);
    }
    return "";
}