Java JFrame fileChooser(JFrame frame, boolean saveDialog, String windowName, String filterName, String filterExtension)

Here you can find the source of fileChooser(JFrame frame, boolean saveDialog, String windowName, String filterName, String filterExtension)

Description

file Chooser

License

Open Source License

Declaration

public static File fileChooser(JFrame frame, boolean saveDialog, String windowName, String filterName,
            String filterExtension) 

Method Source Code


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

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Main {
    public static File fileChooser(JFrame frame, boolean saveDialog, String windowName, String filterName,
            String filterExtension) {
        JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
        fileChooser.setAcceptAllFileFilterUsed(false);
        //fileChooser.setFileFilter(new CustomFileFilter(fileExtension));
        fileChooser.setFileFilter(new FileNameExtensionFilter(filterName, filterExtension));
        File file = null;//from  ww w  .j  a v a  2 s . c om
        if (fileChooser.showDialog(frame, windowName) == JFileChooser.APPROVE_OPTION) {

            String ext = extension(fileChooser.getSelectedFile());
            String message = null;
            if (saveDialog)
                message = windowName.toLowerCase() + " to";
            else
                message = windowName.toLowerCase() + " from";
            if (ext == null) {
                if (saveDialog) {
                    String fileName = fileChooser.getSelectedFile().getAbsolutePath();
                    file = new File(fileName + "." + filterExtension);
                } else
                    JOptionPane.showMessageDialog(frame,
                            "Choose file with extension\n" + filterExtension + "\n to " + message + "!",
                            "Eror choosing file to " + message, JOptionPane.ERROR_MESSAGE);
            } else if (!filterExtension.equals(ext)) {
                JOptionPane.showMessageDialog(frame,
                        "Choose file with extension\n" + filterExtension + "\n to " + message + "!",
                        "Eror choosing file to " + message, JOptionPane.ERROR_MESSAGE);
            } else
                file = fileChooser.getSelectedFile();

        }
        return file;
    }

    private static String extension(File file) {
        int index = file.getName().lastIndexOf(".") + 1;
        if (index <= 0)
            return null;
        return file.getName().substring(index);
    }
}

Related

  1. drawHelpPanel(String helpToDisplay, final JFrame frame)
  2. enable(final JFrame frame)
  3. enterFullScreenIfPossible(JFrame mainAppFrame)
  4. error(JFrame frame, String msg)
  5. exitFullScreen(JFrame mainAppFrame)
  6. findFileOpen(JFrame frame, String ext, String desc)
  7. findFileSave(JFrame frame, String ext, String desc)
  8. findJFrame(Component c)
  9. findJFrame(Component myComponent)