Java JFileChooser setFile(File file, JFileChooser chooser)

Here you can find the source of setFile(File file, JFileChooser chooser)

Description

Set JFileChooser so it will always make the best of going to the location of the input file (whether its a file or directory)

License

Open Source License

Parameter

Parameter Description
file a parameter
chooser a parameter

Declaration

public static void setFile(File file, JFileChooser chooser) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Open Door Logistics (www.opendoorlogistics.com)
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at http://www.gnu.org/licenses/lgpl.txt
 ******************************************************************************/

import java.io.File;

import javax.swing.JFileChooser;

public class Main {
    /**//w w  w .  j a  va 2s.  c  o m
     * Set JFileChooser so it will always make the best of going
     * to the location of the input file (whether its a file or directory)
     * @param file
     * @param chooser
     */
    public static void setFile(File file, JFileChooser chooser) {
        if (file == null) {
            return;
        }

        // try turning into a directory if its a file that doesn't exist
        if (file.exists() == false && file.isFile()) {
            file = file.getParentFile();
        }

        if (file.exists()) {
            if (file.isFile()) {
                chooser.setSelectedFile(file);
            } else if (file.isDirectory()) {
                chooser.setCurrentDirectory(file);
            }
        }

    }
}

Related

  1. selectFile(Component parent, boolean isOpen)
  2. selectFile(String msg, File dir, FileFilter filterOrNull)
  3. selectFileForOpen(final Component parent, final FileFilter[] fileFilters, final String title, final FileFilter[] selectedFilters, final File initFile)
  4. selectNonExistingFile(Component parent, String extensionWanted)
  5. setCurrentDirectory(JFileChooser fileChooser, String location)
  6. setFileChooserFilters(JFileChooser fileDlg, String filters, int selectIndex)
  7. setSelectedFile(JFileChooser chooser, File file)
  8. SHOW_JFILECHOOSER(String title, String approvetext, int fileselectionmode, boolean multiselection)
  9. showFileChooserLoop(Component parentComponent, JFileChooser chooser)