Example usage for javax.swing JFileChooser getSelectedFile

List of usage examples for javax.swing JFileChooser getSelectedFile

Introduction

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

Prototype

public File getSelectedFile() 

Source Link

Document

Returns the selected file.

Usage

From source file:Main.java

public static File chooseDirectory(Component parentComponent, String title) {
    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    jfc.showDialog(parentComponent, title);
    return jfc.getSelectedFile();
}

From source file:Main.java

public static File promptForImageFile(JPanel parent) {
    JFileChooser chooser = new JFileChooser();
    int returnVal = chooser.showOpenDialog(parent);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
        return chooser.getSelectedFile();
    }//w  w  w  .jav  a 2s.c o  m
    return null;
}

From source file:Main.java

public static int showFileChooserLoop(Component parentComponent, JFileChooser chooser) {
    for (;;) {/*  w w w .  j  ava 2s  .  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:Main.java

/**
 * // w  w w  . j  a  va 2s  . co m
 * @param owner
 * @return El directorio seleccionado
 */
public static File saveFileChooser(Window owner) {
    int userResponse;
    JFileChooser fileChooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("XML", "xml");
    fileChooser.setFileFilter(filter);
    userResponse = fileChooser.showSaveDialog(owner);
    if (JFileChooser.APPROVE_OPTION == userResponse) {
        return fileChooser.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   w  ww.ja v a2s  .com*/
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setApproveButtonText("Select Dir");
    return chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION ? chooser.getSelectedFile() : null;
}

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);
    }/*from   ww  w .jav a  2 s.co  m*/
    return 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();/*w  w w .  j a v a2 s .  c om*/
        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 w  w w. j a va2s  .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:Main.java

/**
 * opens a file using a JFileChooser//from w  w  w  .  j  a  v a 2 s . c  o  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:my.grafos.Maquina.java

static void abrirGrafo() throws FileNotFoundException, IOException, ClassNotFoundException {
    /*       FileInputStream fis = new FileInputStream("d:\\t.xml");
    ObjectInputStream ois = new ObjectInputStream(fis);
    ArrayList<Aresta> leituraListaistaArestas = (ArrayList<Aresta>) ois.readObject();
    ois.close();//from  ww w . jav a 2 s . co m
    Fabrica.listaArestas = leituraListaistaArestas;
     */

    a = 0;
    final JFileChooser fc = new JFileChooser();
    //Handle open button action.
    int returnVal = fc.showOpenDialog(null);
    File file = fc.getSelectedFile();
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        file = fc.getSelectedFile();
        //This is where a real application would open the file.
        System.out.println("Opening: " + file.getName() + "." + "\n");

    } else {
        System.out.println("Open command cancelled by user." + "\n");
    }
    StringBuilder arestaBuilder = new StringBuilder();
    StringBuilder verticeBuilder = new StringBuilder();
    String line;
    try {
        InputStream inputstream = new BufferedInputStream(new FileInputStream(file.getPath()));
        BufferedReader r = new BufferedReader(new InputStreamReader(inputstream));
        while ((line = r.readLine()) != null && !line.contains("</list>")) {
            arestaBuilder.append(line);
            arestaBuilder.append("\n");
        }
        arestaBuilder.append("</list>");
        verticeBuilder.append("<list>\n");

        while ((line = r.readLine()) != null) {
            verticeBuilder.append(line);
            verticeBuilder.append("\n");
        }
    } catch (IOException e) {
    }
    Fabrica.xmlA = arestaBuilder.toString();
    Fabrica.xmlV = verticeBuilder.toString();
    Fabrica.listaArestas = (ArrayList<Aresta>) xstream.fromXML(xmlA);
    Fabrica.listaVertices = (ArrayList<Vertice>) xstream.fromXML(xmlV);
}