Java JFrame chooseFile(String directoryPath, String dialogTitle, String selectedFileName, boolean saveDialog, String[] extensions, JFrame parentFrame)

Here you can find the source of chooseFile(String directoryPath, String dialogTitle, String selectedFileName, boolean saveDialog, String[] extensions, JFrame parentFrame)

Description

choose File

License

Open Source License

Declaration

public static File chooseFile(String directoryPath, String dialogTitle, String selectedFileName,
            boolean saveDialog, String[] extensions, JFrame parentFrame) 

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.filechooser.FileNameExtensionFilter;

public class Main {
    public static File chooseFile(String directoryPath, String dialogTitle, String selectedFileName,
            boolean saveDialog, String[] extensions, JFrame parentFrame) {
        File file = null;/* w w w  .j  a  v  a  2  s .  c o  m*/
        JFileChooser fc = null;
        if (directoryPath != null) {
            fc = new JFileChooser(directoryPath);
        } else {
            fc = new JFileChooser();
        }
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fc.setMultiSelectionEnabled(false);
        fc.setSelectedFile(new File(selectedFileName));
        if (extensions != null) {
            fc.setAcceptAllFileFilterUsed(false);
            fc.setFileFilter(makeFileNameExtensionFilter(extensions));
        }
        fc.setDialogTitle(dialogTitle);
        int returnVal = 0;
        if (saveDialog) {
            returnVal = fc.showSaveDialog(parentFrame);
        } else {
            returnVal = fc.showOpenDialog(parentFrame);
        }
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile();
        }
        return file;
    }

    public static FileNameExtensionFilter makeFileNameExtensionFilter(String[] extensions) {
        if (extensions == null)
            return null;
        int numExtensions = 0;
        for (int ii = 0; ii < extensions.length; ++ii)
            if (extensions[ii] != null)
                numExtensions = numExtensions + 1;
        String[] nne = new String[numExtensions];
        int iext = 0;
        String extensionDesc = "";
        for (int ii = 0; ii < extensions.length; ++ii) {
            if (extensions[ii] != null) {
                nne[iext] = extensions[ii];
                if (iext == 0)
                    extensionDesc = extensionDesc + "*." + extensions[ii];
                if (iext > 0)
                    extensionDesc = extensionDesc + ", *." + extensions[ii];
                iext = iext + 1;
            }
        }
        if (numExtensions == 1)
            return new FileNameExtensionFilter(extensionDesc, nne[0]);
        if (numExtensions == 2)
            return new FileNameExtensionFilter(extensionDesc, nne[0], nne[1]);
        if (numExtensions == 3)
            return new FileNameExtensionFilter(extensionDesc, nne[0], nne[1], nne[2]);
        if (numExtensions >= 4)
            return new FileNameExtensionFilter(extensionDesc, nne[0], nne[1], nne[2], nne[3]);
        return null;
    }
}

Related

  1. calculateMaxMenuItems(JFrame window)
  2. centre(JFrame frame)
  3. centreJustifyFrame(final JFrame frame, final int y_position)
  4. centreWindow(Window c, JFrame frame)
  5. changeLookAndFeel(final String lookName, final JFrame frame)
  6. ChooserDemo(JFrame jf, Component comp)
  7. closeFrame(JFrame frame, JProgressBar progressBar)
  8. closeOnEsc(JFrame frame)
  9. closeOnEscape(final JFrame frame)