Java JFileChooser createChooser(String lastPath, final boolean checkOverrideFile)

Here you can find the source of createChooser(String lastPath, final boolean checkOverrideFile)

Description

create Chooser

License

Open Source License

Declaration

public static JFileChooser createChooser(String lastPath, final boolean checkOverrideFile) 

Method Source Code

//package com.java2s;
/**/*  w  ww . j av a 2  s  .co  m*/
 * ============================================================================================
 * Menthor Editor -- Copyright (c) 2015 
 *
 * This file is part of Menthor Editor. Menthor Editor is based on TinyUML and as so it is 
 * distributed under the same license terms.
 *
 * Menthor Editor is free software; you can redistribute it and/or modify it under the terms 
 * of the GNU General Public License as published by the Free Software Foundation; either 
 * version 2 of the License, or (at your option) any later version.
 *
 * Menthor Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with Menthor Editor; 
 * if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 
 * MA  02110-1301  USA
 * ============================================================================================
 */

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Main {
    public static JFileChooser createChooser(String lastPath, final boolean checkOverrideFile) {
        return new JFileChooser(lastPath) {
            private static final long serialVersionUID = 1L;

            @Override
            public void approveSelection() {
                File f = getSelectedFile();
                if (f.exists() && checkOverrideFile) {
                    int result = JOptionPane.showConfirmDialog(this,
                            "\"" + f.getName() + "\" already exists. Do you want to overwrite it?", "Existing file",
                            JOptionPane.YES_NO_CANCEL_OPTION);
                    switch (result) {
                    case JOptionPane.YES_OPTION:
                        super.approveSelection();
                        return;
                    case JOptionPane.NO_OPTION:
                        return;
                    case JOptionPane.CLOSED_OPTION:
                        return;
                    case JOptionPane.CANCEL_OPTION:
                        cancelSelection();
                        return;
                    }
                }
                super.approveSelection();
            }
        };
    }
}

Related

  1. choseFile(File selectedFile, String startDir, final String description, final String extension, Component parent, boolean addExtension, boolean forLoad)
  2. cleanFileSelector(JFileChooser filterUpdates)
  3. cleanFileSelector(JFileChooser filterUpdates)
  4. configureFileChooser(JFileChooser fchooser, boolean directoryOnly, File selectedFile)
  5. correctSelectedFileExtension(JFileChooser chooser)
  6. createFileChooser(File presetFile, final String... postfixes)
  7. createFileChooser(String initDir)
  8. createFileChooser(String title, File dir, String filter)
  9. createJFileChooser(String name)