Example usage for javax.swing JFileChooser showDialog

List of usage examples for javax.swing JFileChooser showDialog

Introduction

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

Prototype

@SuppressWarnings("deprecation")
public int showDialog(Component parent, String approveButtonText) throws HeadlessException 

Source Link

Document

Pops a custom file chooser dialog with a custom approve button.

Usage

From source file:Main.java

public static void main(String[] args) {
    FileNameExtensionFilter extFilter = new FileNameExtensionFilter("Java Source  File", "java", "jav");
    // Set the file filter
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.addChoosableFileFilter(extFilter);

    int returnValue = fileChooser.showDialog(null, "Attach");
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        // Process the file
    }/*from  w w w  .  j  a  v a 2 s . 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.setDragEnabled(true);//from w ww  . java 2 s. c  o  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();
}

From source file:Main.java

public static void main(String[] args) {
    // Create a file filter to show only a directory or .doc files
    FileFilter filter = new FileFilter() {
        @Override//  w  w  w.  ja  v a  2s . c  o m
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }

            String fileName = f.getName().toLowerCase();
            if (fileName.endsWith(".doc")) {
                return true;
            }

            return false; // Reject any other files
        }

        @Override
        public String getDescription() {
            return "Word Document";
        }
    };

    // Set the file filter
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileFilter(filter);

    int returnValue = fileChooser.showDialog(null, "Attach");
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        // Process the file
    }

}

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.resetChoosableFileFilters();
    chooser.showDialog(new JFrame(""), null);
    File curFile = chooser.getSelectedFile();
}

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.setSelectedFiles(new File[] { f });
    chooser.showDialog(new JFrame(""), null);
    File curFile = chooser.getSelectedFile();
}

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.setDialogType(JFileChooser.SAVE_DIALOG);
    chooser.showDialog(new JFrame(""), null);
    File curFile = chooser.getSelectedFile();
}

From source file:FileChooserDialog.java

public static void main(String[] args) {
    JFileChooser fileopen = new JFileChooser();
    FileFilter filter = new FileNameExtensionFilter("c files", "c");
    fileopen.addChoosableFileFilter(filter);

    int ret = fileopen.showDialog(null, "Open file");

    if (ret == JFileChooser.APPROVE_OPTION) {
        File file = fileopen.getSelectedFile();
        System.out.println(file);
    }/*www  .  j  a va  2  s.  c  o  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.setFileSystemView(FileSystemView.getFileSystemView());
    chooser.showDialog(new JFrame(""), null);
    File curFile = chooser.getSelectedFile();
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    final JButton button = new JButton("Select files...");
    button.addActionListener(e -> {/*from ww  w .  j av a2s .c  om*/
        final JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(chooser.getFileSystemView().getParentDirectory(new File("C:\\")));
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.showDialog(button, "Select file");
    });
    panel.add(button);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}