Example usage for javax.swing JFileChooser showDialog

List of usage examples for javax.swing JFileChooser showDialog

Introduction

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

Prototype

@SuppressWarnings("deprecation")
public int showDialog(Component parent, String approveButtonText) throws HeadlessException 

Source Link

Document

Pops a custom file chooser dialog with a custom approve button.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JFileChooser fc = new JFileChooser();
    fc.setMultiSelectionEnabled(true);//from   ww  w  .java 2s . co  m
    fc.setCurrentDirectory(new File("C:\\tmp"));
    JButton btn1 = new JButton("Show Dialog");
    btn1.addActionListener(e -> fc.showDialog(frame, "Choose"));
    JButton btn2 = new JButton("Show Open Dialog");
    btn2.addActionListener(e -> {
        int retVal = fc.showOpenDialog(frame);
        if (retVal == JFileChooser.APPROVE_OPTION) {
            File[] selectedfiles = fc.getSelectedFiles();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < selectedfiles.length; i++) {
                sb.append(selectedfiles[i].getName());
                sb.append("\n");
            }
            JOptionPane.showMessageDialog(frame, sb.toString());
        }
    });
    JButton btn3 = new JButton("Show Save Dialog");
    btn3.addActionListener(e -> fc.showSaveDialog(frame));
    Container pane = frame.getContentPane();
    pane.setLayout(new GridLayout(3, 1, 10, 10));
    pane.add(btn1);
    pane.add(btn2);
    pane.add(btn3);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static File chooseFile(Component parentComponent, String title) {
    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    jfc.showDialog(parentComponent, title);
    return jfc.getSelectedFile();
}

From source file:Main.java

public static File chooseDirectory(Component parentComponent, String title) {
    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    jfc.showDialog(parentComponent, title);
    return jfc.getSelectedFile();
}

From source file:Main.java

/**
 * opens a file using a JFileChooser//from   w ww  .  j  a v a2s .c  o m
 *
 * @param c          parent
 * @param mode       selection Mode {@link JFileChooser}
 * @param buttonText buttonText, uses "Open" when null ({@link JFileChooser})
 * @return File
 */
@SuppressWarnings({ "SameParameterValue", "WeakerAccess" })
public static File openJFileChooser(Component c, File currentDirectory, int mode, String buttonText) {
    JFileChooser fc = new JFileChooser(currentDirectory);
    fc.setFileSelectionMode(mode);
    int result;
    if (buttonText != null) {
        result = fc.showDialog(c, buttonText);
    } else {
        result = fc.showOpenDialog(c);
    }
    if (result == JFileChooser.APPROVE_OPTION) {
        return fc.getSelectedFile();
    }
    return null;
}

From source file:com.stacksync.desktop.util.FileUtil.java

private static File showBrowseDialogDefault(int jFileChooserFileSelectionMode) {
    JFileChooser fc = new JFileChooser();

    fc.setFileSelectionMode(jFileChooserFileSelectionMode);

    if (fc.showDialog(null, "Select") != JFileChooser.APPROVE_OPTION) {
        return null;
    }//from ww  w  . j ava2 s  .  c o  m

    return fc.getSelectedFile();
}

From source file:net.menthor.editor.v2.util.Util.java

public static File chooseFile(Component parent, String lastPath, String dialogTitle, String fileDescription,
        String fileExtension, boolean checkOverrideFile) throws IOException {
    JFileChooser fileChooser = createChooser(lastPath, checkOverrideFile);
    fileChooser.setDialogTitle(dialogTitle);
    FileNameExtensionFilter filter = new FileNameExtensionFilter(fileDescription, fileExtension);
    fileChooser.addChoosableFileFilter(filter);
    if (SystemUtil.onWindows())
        fileChooser.setFileFilter(filter);
    fileChooser.setAcceptAllFileFilterUsed(false);
    if (fileChooser.showDialog(parent, "Ok") == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();
        if (!file.getName().endsWith("." + fileExtension)) {
            file = new File(file.getCanonicalFile() + "." + fileExtension);
        } else {/* w  w w  . j  a va 2s  .c  om*/
            file = new File(file.getCanonicalFile() + "");
        }
        return file;
    } else {
        return null;
    }
}

From source file:at.tuwien.ifs.somtoolbox.apps.viewer.fileutils.ExportUtils.java

public static void saveMapPaneAsImage(Container container, JFileChooser fileChooser,
        GenericPNodeScrollPane mapPane, String title) {
    JFileChooser fc = ExportUtils.getFileChooser(container, fileChooser, new JCheckBox("Crop", true));
    int returnVal = fc.showDialog(container, title);
    File filePath = null;//from   w  ww .  j a v a2s  . c  o  m

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        filePath = fc.getSelectedFile();
    }
    if (filePath != null) {
        try {
            ExportUtils.saveMapPaneAsImage(mapPane, filePath.getAbsolutePath(),
                    ((JCheckBox) fc.getAccessory()).isSelected());
            JOptionPane.showMessageDialog(container, "Export to file finished!");
        } catch (SOMToolboxException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(container, ex.getMessage(), "Error saving",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}

From source file:net.menthor.editor.v2.util.Util.java

public static File chooseFile(Component parent, String lastPath, String dialogTitle, String fileDescription,
        String fileExtension, String fileExtension2, boolean checkOverrideFile) throws IOException {
    JFileChooser fileChooser = createChooser(lastPath, checkOverrideFile);
    fileChooser.setDialogTitle(dialogTitle);
    FileNameExtensionFilter filter = new FileNameExtensionFilter(fileDescription, fileExtension,
            fileExtension2);//w w w.  j a va 2 s.c  om
    fileChooser.addChoosableFileFilter(filter);
    if (SystemUtil.onWindows())
        fileChooser.setFileFilter(filter);
    fileChooser.setAcceptAllFileFilterUsed(false);
    if (fileChooser.showDialog(parent, "Ok") == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();
        if (!(file.getName().endsWith("." + fileExtension))
                && !(file.getName().endsWith("." + fileExtension2))) {
            file = new File(file.getCanonicalFile() + "." + fileExtension2);
        } else {
            file = new File(file.getCanonicalFile() + "");
        }
        return file;
    } else {
        return null;
    }
}

From source file:at.tuwien.ifs.somtoolbox.apps.viewer.fileutils.ExportUtils.java

public static File getFilePath(Component parent, JFileChooser fileChooser, String title, String defaultFileName,
        FileFilter filter) {/*from w w  w  .  j  a  va 2  s.  com*/
    fileChooser = initFileChooser(fileChooser, filter);
    if (org.apache.commons.lang.StringUtils.isNotBlank(defaultFileName)) {
        fileChooser.setSelectedFile(new File(defaultFileName));
    }
    int returnVal = fileChooser.showDialog(parent, title);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        return fileChooser.getSelectedFile();
    } else {
        return null;
    }
}

From source file:it.unibas.spicygui.controllo.tree.ActionExportQuery.java

public void actionPerformed(ActionEvent e) {
    this.executeInjection();
    JFileChooser chooser = vista.getFileChooserSalvaFileGenerico();
    File file;/*from ww  w. j  ava2 s . c o m*/
    int returnVal = chooser.showDialog(WindowManager.getDefault().getMainWindow(),
            NbBundle.getMessage(Costanti.class, Costanti.EXPORT));
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        FileWriter writer = null;
        try {
            file = chooser.getSelectedFile();
            writer = new FileWriter(file);
            writer.write(this.textArea.getText());
            writer.flush();
            StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(Costanti.class, Costanti.EXPORT_OK));
        } catch (IOException ex) {
            DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
                    NbBundle.getMessage(Costanti.class, Costanti.EXPORT_ERROR) + " : " + ex.getMessage(),
                    DialogDescriptor.ERROR_MESSAGE));
            logger.error(ex);
        } finally {
            try {
                writer.close();
            } catch (IOException ex) {
            }
        }
    }
}