Java JDialog chooseFile(final JDialog dialog, final String title)

Here you can find the source of chooseFile(final JDialog dialog, final String title)

Description

choose File

License

Open Source License

Declaration

static File chooseFile(final JDialog dialog, final String title) 

Method Source Code


//package com.java2s;
/*//w w  w.  j ava 2 s.  c  o m
    
dsh-variation-cytoscape3-app  Variation Cytoscape3 app.
Copyright (c) 2013-2015 held jointly by the individual authors.
    
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.
    
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.
    
You should have received a copy of the GNU Lesser General Public License
along with this library;  if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
    
> http://www.fsf.org/licensing/licenses/lgpl.html
> http://www.opensource.org/licenses/lgpl-license.php
    
*/

import java.awt.FileDialog;

import java.io.File;
import java.io.FilenameFilter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JDialog;

public class Main {
    static File chooseFile(final JDialog dialog, final String title) {
        return chooseFile(dialog, title, null);
    }

    static File chooseFile(final JDialog dialog, final String title, final String fileNamePattern) {
        // awt file chooser
        FileDialog fileDialog = new FileDialog(dialog, title, FileDialog.LOAD);
        //fileDialog.setMultipleMode(false); jdk 1.7+
        if (fileNamePattern != null) {
            final Pattern pattern = Pattern.compile(fileNamePattern);
            fileDialog.setFilenameFilter(new FilenameFilter() {
                @Override
                public boolean accept(final File directory, final String name) {
                    Matcher m = pattern.matcher(name);
                    return m.matches();
                }
            });
        }

        // workaround for apple 1.6 jdk bug on osx
        String fileDialogForDirectories = System.getProperty("apple.awt.fileDialogForDirectories");
        System.setProperty("apple.awt.fileDialogForDirectories", "false");
        fileDialog.setVisible(true);
        System.setProperty("apple.awt.fileDialogForDirectories", fileDialogForDirectories);

        // jdk 1.6
        String directoryName = fileDialog.getDirectory();
        String fileName = fileDialog.getFile();
        if (directoryName != null && fileName != null) {
            return new File(directoryName, fileName);
        }
        return null;

        // null-safe jdk 1.7+
        /*
          if (fileDialog.getFiles().length > 0)
          {
        model.setSequenceFile(fileDialog.getFiles()[0]);
          }
        */

        /*
        // swing file chooser
        JFileChooser fileChooser = new JFileChooser();
        if (JFileChooser.APPROVE_OPTION == fileChooser.showOpenDialog(WormPlotApp.this))
        {
          model.setSequenceFile(fileChooser.getSelectedFile());
        }
        */
    }
}

Related

  1. addFileOnAction(AbstractButton button, final JTextField textField, final JDialog parent)
  2. alignDialogInMiddleOfScreen(JDialog dlg)
  3. applyDefaultProperties(final JDialog comp)
  4. calculatePosition(Window parent, JDialog dialog)
  5. cascadeDialog(JDialog dialog, JDialog parent)
  6. closeDialog(final JDialog dialog)
  7. commonFileDialog(JDialog parent, boolean save)
  8. configureCancelForDialog(final JDialog dialog, JButton cancelButton)
  9. decorate(JDialog dialog, int type)