Example usage for javax.swing JFileChooser showOpenDialog

List of usage examples for javax.swing JFileChooser showOpenDialog

Introduction

In this page you can find the example usage for javax.swing JFileChooser showOpenDialog.

Prototype

public int showOpenDialog(Component parent) throws HeadlessException 

Source Link

Document

Pops up an "Open File" file chooser dialog.

Usage

From source file:Main.java

public static String browseForFile(Window owner, String file, int selectionMode, String title,
        FileFilter filter) {// w w w .  ja  v  a  2s . c  o  m
    final String curDir = System.getProperty("user.dir");
    JFileChooser chooser = new JFileChooser(curDir);
    chooser.setDialogType(JFileChooser.OPEN_DIALOG);
    chooser.setFileSelectionMode(selectionMode);
    chooser.setApproveButtonText("Select");
    chooser.setApproveButtonMnemonic('s');
    chooser.setDialogTitle(title);
    if (filter != null)
        chooser.setFileFilter(filter);

    if (file != null && !file.isEmpty()) {
        File curFile = new File(file);

        chooser.setCurrentDirectory(curFile.getAbsoluteFile().getParentFile());

        if (curFile.isDirectory()) {
            try {
                chooser.setSelectedFile(curFile.getCanonicalFile());
            } catch (IOException ex) {
            }
        } else {
            chooser.setSelectedFile(curFile);
        }
    }

    if (chooser.showOpenDialog(owner) == JFileChooser.APPROVE_OPTION) {
        String path = chooser.getSelectedFile().getPath();
        try {
            path = new File(path).getCanonicalPath();
        } catch (IOException e) {
        }
        // make path relative if possible
        if (path.startsWith(curDir)) {
            path = "." + path.substring(curDir.length());
        }

        return path;
    }
    return null;
}

From source file:com.igormaznitsa.ideamindmap.utils.IdeaUtils.java

public static File chooseFile(final Component parent, final boolean filesOnly, final String title,
        final File selectedFile, final FileFilter filter) {
    final JFileChooser chooser = new JFileChooser(selectedFile);

    chooser.setApproveButtonText("Select");
    if (filter != null)
        chooser.setFileFilter(filter);/*from  w ww  . j  a va2s  . c  o  m*/

    chooser.setDialogTitle(title);
    chooser.setMultiSelectionEnabled(false);

    if (filesOnly)
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    else
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

    if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
        return chooser.getSelectedFile();
    } else {
        return null;
    }
}

From source file:ch.admin.hermes.etl.load.HermesETLApplication.java

/**
 * FileOpen Dialog//  w  ww  . j a  v  a  2 s .  c  o  m
 * @param title
 * @param name
 * @param ext
 * @return
 */
private static String getFile(final String title, final String[] name, final String[] ext) {
    // User Chooses a Path and Name for the html Output File
    JFileChooser fc = new JFileChooser(".");
    fc.setDialogType(JFileChooser.OPEN_DIALOG);
    fc.setDialogTitle(title);
    fc.setFileFilter(new FileFilter() {
        public String getDescription() {
            StringBuffer str = new StringBuffer();
            for (String n : name)
                str.append(n + "");
            return (str.toString());
        }

        public boolean accept(File f) {
            for (String e : ext)
                if (f.isDirectory() || f.getName().toLowerCase().endsWith(e))
                    return (true);
            return false;
        }
    });

    int state = fc.showOpenDialog(null);

    if (state == JFileChooser.APPROVE_OPTION)
        return (fc.getSelectedFile().getPath());

    return (null);
}

From source file:ActionListenerTest2.java

public void actionPerformed(ActionEvent e) {
    JFileChooser fileChooser = new JFileChooser();
    int returnVal = fileChooser.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        System.out.print(fileChooser.getSelectedFile().getName());
    }//from  ww w. j a v  a  2s. c  om
}

From source file:CoreJavaSound.java

public CoreJavaSound() throws Exception {
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    soundFile = chooser.getSelectedFile();

    System.out.println("Playing " + soundFile.getName());

    Line.Info linfo = new Line.Info(Clip.class);
    Line line = AudioSystem.getLine(linfo);
    clip = (Clip) line;//from www . j  a  v a  2 s  .co m
    clip.addLineListener(this);
    AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
    clip.open(ais);
    clip.start();
}

From source file:Main.java

public Main() throws Exception {
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    soundFile = chooser.getSelectedFile();

    System.out.println("Playing " + soundFile.getName());

    Line.Info linfo = new Line.Info(Clip.class);
    Line line = AudioSystem.getLine(linfo);
    clip = (Clip) line;//from   w  w w . j a v a 2s. co m
    clip.addLineListener(this);
    AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
    clip.open(ais);
    clip.start();
}

From source file:Main.java

/**
 * Displays a {@link JFileChooser} to select a file.
 *
 * @param title The title of the dialog.
 * @param startDirectory The directory where the dialog is initialed opened.
 * @param fileExtension File extension to filter each content of the opened
 * directories. Example ".xml"./*from w w w .  j av  a 2s.c o m*/
 * @param startFile The preselected file where the dialog is initialed opened.
 * @return The {@link File} object selected, returns null if no file was selected.
 */
public static File chooseFile(String title, String startDirectory, final String fileExtension,
        String startFile) {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setDialogTitle(title);

    if (fileExtension != null && !fileExtension.trim().equals("")) {
        FileFilter filter = new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                return pathname.isDirectory() || pathname.getName().endsWith(fileExtension);
            }

            @Override
            public String getDescription() {
                return "(" + fileExtension + ")";
            }
        };

        chooser.setFileFilter(filter);
    }

    if (startDirectory != null && !startDirectory.trim().equals("")) {
        chooser.setCurrentDirectory(new File(startDirectory));
    }

    if (startFile != null && !startFile.trim().equals("")) {
        chooser.setSelectedFile(new File(startFile));
    }

    int status = chooser.showOpenDialog(null);

    if (status == JFileChooser.APPROVE_OPTION) {
        return chooser.getSelectedFile();
    }

    return null;
}

From source file:ImageFileFilterImageScale.java

private String getImageFile() {
    JFileChooser fc = new JFileChooser();
    fc.setFileFilter(new ImageFilter());
    int result = fc.showOpenDialog(null);
    File file = null;// w  w  w.  j a v  a 2  s .c o  m
    if (result == JFileChooser.APPROVE_OPTION) {
        file = fc.getSelectedFile();
        return file.getPath();
    } else
        return null;

}

From source file:GuardarImagen.AyudaParaImagen.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
    JFileChooser selector = new JFileChooser();
    selector.showOpenDialog(this);
    File file = selector.getSelectedFile();
    try {/*from  w  w  w.j av a2s.c  om*/
        FileInputStream fis = new FileInputStream(file);
        byte[] datosImagen = IOUtils.toByteArray(fis);
        Galeria g1 = new Galeria();
        g1.setDatosImagen(datosImagen);
        g1.setDescripcion("Porno 1");
        g1.setTitulo("Madonna");
        PersistenciaGaleria p = new PersistenciaGaleria();
        p.guardar(g1);
    } catch (Exception e) {
    }
}

From source file:it.unibas.spicygui.controllo.file.ActionCsvFileChooserSchema.java

public void actionPerformed(ActionEvent e) {
    executeInjection();//from  www.ja v a  2  s .co m
    JFileChooser chooser = vista.getFileChooserApriCSV();
    File file;
    int returnVal = chooser.showOpenDialog(WindowManager.getDefault().getMainWindow());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        //giannisk Add schema file path to previous ones
        csvConfigurationPM.addToSchemaPathList(file.getAbsolutePath());
    }
}