Java JFileChooser getOpenFile(String title)

Here you can find the source of getOpenFile(String title)

Description

get Open File

License

Open Source License

Declaration

public static File getOpenFile(String title) 

Method Source Code


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

import java.awt.Dimension;
import java.io.File;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Main {
    public static File getOpenFile(String title) {
        JFileChooser chooser = getChooser(title);
        int result = chooser.showOpenDialog(null);

        if (result == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            System.out.println("User chose file " + file.getAbsolutePath());
            if (!file.exists()) {
                showMessage("File " + file.getName() + " doesn't exist, aborting");
                return null;
            }//from   ww  w . j a v  a  2  s .c  o m
            return file;
        } else {
            showMessage("You must choose a file to load from");
            return null;
        }
    }

    public static JFileChooser getChooser(String title) {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle(title);
        chooser.setPreferredSize(new Dimension(800, 600));
        chooser.getActionMap().get("viewTypeDetails").actionPerformed(null);
        return chooser;
    }

    public static void showMessage(Object message) {
        JOptionPane pane = new JOptionPane(message, JOptionPane.WARNING_MESSAGE);
        JDialog dialog2 = pane.createDialog(null, "Message");
        dialog2.setAlwaysOnTop(true);
        dialog2.setVisible(true);
    }
}

Related

  1. getLoadFilename(Component parent)
  2. getLoadFiles(String message, File openDefaultDirectory, String description, String... extensions)
  3. getNewFileChooser()
  4. getOpenFile(String defaultPath)
  5. getOpenFile(String name, String currentDirectory)
  6. getPropertiesFile(boolean saving, String startName, String extension, String description)
  7. getSaveAsFile(String defaultName, String currentDirectory, String defaultExtension)
  8. getSaveFile(String message, File defaultFileOrDir, String description, final String... extensions)
  9. getSelectedFiles(final JFileChooser chooser)