Java Utililty Methods JFileChooser

List of utility methods to do JFileChooser

Description

The list of methods to do JFileChooser are organized into topic(s).

Method

JFileChoosergetFileChooser()
get File Chooser
if (fileChooser == null) {
    fileChooser = new JFileChooser();
return fileChooser;
JFileChoosergetFileChooser(Component parent, JFileChooser fileChooser, JComponent accessory)
get File Chooser
fileChooser = initFileChooser(fileChooser, null);
fileChooser.setAccessory(accessory);
return fileChooser;
FilegetFileChooserSelectedFile(JFileChooser fchooser, boolean directoryOnly, String fileName)
Generates the selected file for the given JFileChooser based on the directoryOnly and fileName parameters.
File targetSelectedFile;
File currentSelectedFile = fchooser.getSelectedFile();
if (currentSelectedFile == null) {
    if (directoryOnly) {
        targetSelectedFile = fchooser.getCurrentDirectory();
    } else {
        targetSelectedFile = new File(fchooser.getCurrentDirectory(), fileName);
} else {
    if (directoryOnly) {
        targetSelectedFile = currentSelectedFile.isDirectory() ? currentSelectedFile
                : currentSelectedFile.getParentFile();
    } else {
        File dir = currentSelectedFile.isDirectory() ? currentSelectedFile
                : currentSelectedFile.getParentFile();
        targetSelectedFile = new File(dir, fileName);
return targetSelectedFile;
FilegetFileFromChooserForPNG()
get File From Chooser For PNG
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("save panel as a png file");
chooser.setSize(new java.awt.Dimension(45, 37)); 
chooser.setFileFilter(new FileNameExtensionFilter("png", "png"));
File file = new File("C:\\deneme.png");
if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
    file = chooser.getSelectedFile();
    if (file.getName().indexOf(".png") == -1) {
...
FilegetFileFromChooserSave()
get File From Chooser Save
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("save as file");
chooser.setSize(new java.awt.Dimension(45, 37)); 
File file = null;
if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
    file = chooser.getSelectedFile();
    return file;
return file;
StringgetFileName(Component parent)
get file name
if (mFileChoose == null) {
    mFileChoose = new JFileChooser();
    mFileChoose.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal = mFileChoose.showOpenDialog(parent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
    return mFileChoose.getSelectedFile().getAbsolutePath();
} else {
...
StringgetFileOrDir(String startName, boolean fileSelect)
Open a JFileChooser to select a filename or directory starting with startName
String selection = "";
JFileChooser fc = createFileChooser(fileSelect, startName, false);
fc.setMultiSelectionEnabled(false);
int returnVal = -1;
returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
    selection = fc.getSelectedFile().getAbsolutePath();
return selection;
StringgetFilePath()
This method shows a dialog where a file can be selected and returns the file's path afterwards.
String filePath = "";
try {
    final JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fileChooser.setDialogTitle(FILEDIALOG_TITLE);
    int returnVal = fileChooser.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File chosenFile = fileChooser.getSelectedFile();
...
FilegetFilePath(Component parent, JFileChooser fileChooser, String title, FileFilter filter)
get File Path
fileChooser = initFileChooser(fileChooser, filter);
int returnVal = fileChooser.showDialog(parent, title);
if (returnVal == JFileChooser.APPROVE_OPTION) {
    return fileChooser.getSelectedFile();
} else {
    return null;
String[][]getFiles(String title, String initialRoot, String initialFile)
get Files
if (title == null || title == "")
    title = "Choose files";
if (initialRoot == null || initialRoot == "")
    initialRoot = "E:\\Data";
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
FileNameExtensionFilter filter = new FileNameExtensionFilter("Tiff files", "tif", "tiff");
chooser.setFileFilter(filter);
...