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 void main(String s[]) {
    JFileChooser chooser = new JFileChooser();
    int rc = chooser.showOpenDialog(null);
    while (rc == JFileChooser.APPROVE_OPTION && !chooser.getSelectedFile().getName().endsWith(".java")) {
        JOptionPane.showMessageDialog(null,
                "The file " + chooser.getSelectedFile() + " is not java source file.", "Open Error",
                JOptionPane.ERROR_MESSAGE);
        rc = chooser.showOpenDialog(null);
    }/*from  ww  w . j a  v a 2s  .  co  m*/
}

From source file:Main.java

public static void main(String[] argv) {
    String filename = File.separator + "tmp";
    JFileChooser fc = new JFileChooser(new File(filename));

    // Show open dialog
    fc.showOpenDialog(null);//from w w  w.jav  a2s  .  c om
    File selFile = fc.getSelectedFile();

    // Show save dialog
    fc.showSaveDialog(null);
    selFile = fc.getSelectedFile();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFileChooser fileChooser = new JFileChooser();
    int a = fileChooser.showOpenDialog(null);

    if (a == JFileChooser.APPROVE_OPTION) {
        File fileToOpen = fileChooser.getSelectedFile();
        Desktop.getDesktop().open(fileToOpen);
    }//w ww .j a  v  a 2s .co m
}

From source file:JFileChooserSelectionOption.java

public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser(".");
    int status = fileChooser.showOpenDialog(null);

    if (status == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        System.out.println(selectedFile.getParent());
        System.out.println(selectedFile.getName());
    } else if (status == JFileChooser.CANCEL_OPTION) {
        System.out.println("canceled");
    }//from w  w  w.  j  a  v a2 s.  co m
}

From source file:Main.java

public static void main(String[] argv) {
    JFileChooser fileChooser = new JFileChooser(new File("."));
    fileChooser.addChoosableFileFilter(new MyFilter());

    fileChooser.showOpenDialog(null);//from w ww .j a  v  a 2 s .com
    System.out.println(fileChooser.getSelectedFile());

}

From source file:Main.java

public static void main(String[] args) {
    // Display an open file chooser

    JFileChooser fileChooser = new JFileChooser();

    int returnValue = fileChooser.showOpenDialog(null);

    if (returnValue == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        System.out.println("we selected: " + selectedFile);
    }/*  w  w  w  .ja  v  a2s  .co  m*/

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFileChooser chooser = new JFileChooser();
    File f = new File(new File("filename.txt").getCanonicalPath());
    chooser.setSelectedFile(f);//from  ww w.j a  v a2 s.  co m
    chooser.showOpenDialog(null);
    File curFile = chooser.getSelectedFile();
}

From source file:Main.java

public static void main(String[] args) {
    String text = "JFileChooser, you're my only friend.";

    JFileChooser chooser = new JFileChooser();
    int result = chooser.showSaveDialog(null);

    if (result == JFileChooser.APPROVE_OPTION) {
        try {//from   w  w  w  . j a va 2s . c  o  m
            File file = chooser.getSelectedFile();
            FileWriter writer = new FileWriter(file);
            writer.write(text);
            writer.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFileChooser chooser = new JFileChooser();
    File f = new File(new File("filename.txt").getCanonicalPath());

    chooser.setDragEnabled(true);/*from w ww  . j a va 2 s.  co m*/
    chooser.showDialog(new JFrame(""), null);
    File curFile = chooser.getSelectedFile();
}

From source file:Main.java

License:asdf

public static void main(String[] argv) throws Exception {
    JFileChooser chooser = new JFileChooser();
    File f = new File(new File("filename.txt").getCanonicalPath());

    chooser.setDialogTitle("asdf");
    chooser.showDialog(new JFrame(""), null);
    File curFile = chooser.getSelectedFile();
}