Select file to load via FileDialog . - Java Swing

Java examples for Swing:JDialog

Description

Select file to load via FileDialog .

Demo Code

/*/*  w ww .  j  a  va 2s  .com*/
 * Copyright (c) 2011, 2020, Frank Jiang and/or its affiliates. All rights
 * reserved. SwingUtils.java is built in 2012-11-2.
 */
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.JTextComponent;

public class Main{
    /**
     * Select file to load via {@linkplain FileDialog}.
     * 
     * @param parent
     *            the parent of {@linkplain FileDialog}
     * @param title
     *            the title of {@linkplain FileDialog}
     * @return the selected file if selected and created, null otherwise
     */
    public static File selectLoadFile(Window parent, String title) {
        return selectLoadFile(null, parent, title, null);
    }
    /**
     * Select file to load via {@linkplain FileDialog}.
     * 
     * @param parent
     *            the parent of {@linkplain FileDialog}
     * @param title
     *            the title of {@linkplain FileDialog}
     * @param filter
     *            Sets the filename filter for this file dialog window to the
     *            specified filter. Filename filters do not function in Sun's
     *            reference implementation for Microsoft Windows.
     * @return the selected file if selected and created, null otherwise
     */
    public static File selectLoadFile(Window parent, String title,
            FilenameFilter filter) {
        return selectLoadFile(null, parent, title, filter);
    }
    /**
     * Select file to load via {@linkplain FileDialog}.
     * 
     * @param file
     *            the default file or the directory of the default file to set
     *            the default directory of the {@linkplain FileDialog}
     * @param parent
     *            The parent of {@linkplain FileDialog}. <br>
     *            This type can be {@linkplain Dialog} or {@linkplain Frame},
     *            otherwise use <code>null</code> as default.
     * @param title
     *            the title of {@linkplain FileDialog}
     * @param filter
     *            Sets the filename filter for this file dialog window to the
     *            specified filter. Filename filters do not function in Sun's
     *            reference implementation for Microsoft Windows.
     * @return the selected file if selected and created, <code>null</code>
     *         otherwise
     */
    public static File selectLoadFile(File file, Window parent,
            String title, FilenameFilter filter) {
        FileDialog dialog = null;
        if (parent instanceof Dialog)
            dialog = new FileDialog((Dialog) parent, title, FileDialog.LOAD);
        else if (parent instanceof Frame)
            dialog = new FileDialog((Frame) parent, title, FileDialog.LOAD);
        else {
            if (parent != null)
                throw new RuntimeException(String.format(
                        Messages.getString("SwingUtils.12"),////$NON-NLS-1$
                        parent.getClass()));
            else {
                Frame p = null;
                dialog = new FileDialog(p, title, FileDialog.LOAD);
            }
        }
        if (file != null && file.exists()) {
            if (file.isDirectory())
                dialog.setDirectory(file.getAbsolutePath());
            else
                dialog.setDirectory(file.getParent());
        }
        if (filter != null)
            dialog.setFilenameFilter(filter);
        dialog.setVisible(true);
        String filename = dialog.getFile();
        if (filename == null || filename.equals(" "))//$NON-NLS-1$
            return null;
        String dir = dialog.getDirectory();
        dialog.dispose();
        return new File(dir, filename);
    }
    /**
     * Select file to load with specified file filter via
     * {@linkplain JFileChooser}.
     * 
     * @param parent
     *            the parent component
     * @param title
     *            the title of current dialog
     * @param filter
     *            the file filter for the current dialog selection, set null if
     *            is not needed
     * @return the selected file if selected and created, null otherwise
     */
    public static File selectLoadFile(Component parent, String title,
            FileFilter filter) {
        JFileChooser jfc = new JFileChooser();
        jfc.setDialogType(JFileChooser.OPEN_DIALOG);
        jfc.setDialogTitle(title);
        if (filter != null)
            jfc.setFileFilter(filter);
        jfc.setMultiSelectionEnabled(false);
        jfc.showOpenDialog(parent);
        return jfc.getSelectedFile();
    }
}

Related Tutorials