Example usage for javax.swing JFileChooser setDialogType

List of usage examples for javax.swing JFileChooser setDialogType

Introduction

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

Prototype


@BeanProperty(preferred = true, enumerationValues = { "JFileChooser.OPEN_DIALOG", "JFileChooser.SAVE_DIALOG",
        "JFileChooser.CUSTOM_DIALOG" }, description = "The type (open, save, custom) of the JFileChooser.")
public void setDialogType(int dialogType) 

Source Link

Document

Sets the type of this dialog.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFileChooser chooser = new JFileChooser();
    File f = new File(new File("filename.txt").getCanonicalPath());

    chooser.setDialogType(JFileChooser.SAVE_DIALOG);
    chooser.showDialog(new JFrame(""), null);
    File curFile = chooser.getSelectedFile();
}

From source file:Main.java

public static String browseForFile(Window owner, String file, int selectionMode, String title,
        FileFilter filter) {// w  ww .ja  v  a 2 s  .  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:Main.java

public static JFileChooser createFileChooser(String title, FileFilter filter, int mode,
        boolean multiSelectionEnabled, int dialogType) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setDialogTitle(title);//from w  w  w.j  av a2 s  .c om
    fileChooser.setFileFilter(filter);
    fileChooser.setFileSelectionMode(mode);
    fileChooser.setDialogType(dialogType);
    fileChooser.setMultiSelectionEnabled(multiSelectionEnabled);
    fileChooser.setCurrentDirectory(getLastDirectory());
    return fileChooser;
}

From source file:Main.java

/**
 * Displays the given file chooser. Utility method for avoiding of memory leak in JDK
 * 1.3 {@link javax.swing.JFileChooser#showDialog}.
 *
 * @param chooser the file chooser to display.
 * @param parent the parent window./*from w w w.  ja v a  2  s  . com*/
 * @param approveButtonText the text for the approve button.
 *
 * @return the return code of the chooser.
 */
public static final int showJFileChooser(JFileChooser chooser, Component parent, String approveButtonText) {
    if (approveButtonText != null) {
        chooser.setApproveButtonText(approveButtonText);
        chooser.setDialogType(javax.swing.JFileChooser.CUSTOM_DIALOG);
    }

    Frame frame = (parent instanceof Frame) ? (Frame) parent
            : (Frame) javax.swing.SwingUtilities.getAncestorOfClass(java.awt.Frame.class, parent);
    String title = chooser.getDialogTitle();

    if (title == null) {
        title = chooser.getUI().getDialogTitle(chooser);
    }

    final JDialog dialog = new JDialog(frame, title, true);
    dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    Container contentPane = dialog.getContentPane();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(chooser, BorderLayout.CENTER);
    dialog.pack();
    dialog.setLocationRelativeTo(parent);
    chooser.rescanCurrentDirectory();

    final int[] retValue = new int[] { javax.swing.JFileChooser.CANCEL_OPTION };

    ActionListener l = new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            if (ev.getActionCommand() == JFileChooser.APPROVE_SELECTION) {
                retValue[0] = JFileChooser.APPROVE_OPTION;
            }

            dialog.setVisible(false);
            dialog.dispose();
        }
    };

    chooser.addActionListener(l);
    dialog.show();

    return (retValue[0]);
}

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

/**
 * FileOpen Dialog//from w  w w  . j av a2s.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:grafix.principal.Comandos.java

static public void cmdSalvarConfiguracao() {
    ControleRegistro.alertaRegistro();/*from  w  w  w.  j  a v  a 2s  .c o  m*/
    JFileChooser chooser = new JFileChooser(new File(ConfiguracoesGrafix.PASTA_TEMPLATES));
    chooser.setSelectedFile(new File(ConfiguracoesGrafix.EXTENSAO_TEMPLATES));
    chooser.setDialogType(JFileChooser.SAVE_DIALOG);
    int returnVal = chooser.showSaveDialog(Controle.getTela());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        try {
            Controle.getConfiguracoesUsuario().setNome(file.getName());
            Controle.salvarConfiguracoesUsuario(true);
            LeitorArquivoConfiguracao.getInstance().criarCopia(file);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    Controle.getTela().getComboConfiguracoes().popularCombo();
}

From source file:com.decypher.threadsclient.JPanelChart.java

private String getFolder() {
    JFileChooser folderPick = new JFileChooser();
    folderPick.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    folderPick.setDialogType(JFileChooser.SAVE_DIALOG);
    folderPick.setMultiSelectionEnabled(false);
    String selected;//from w  w  w  .ja va2s  . c o m
    int returnVal = folderPick.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        selected = folderPick.getSelectedFile().getPath();
    } else {
        selected = "";
    }
    return selected;
}

From source file:cool.pandora.modeller.ui.handlers.base.AddDataHandler.java

/**
 * addData.//ww  w . j  a v  a 2 s.com
 */
void addData() {
    final File selectFile = new File(File.separator + ".");
    final JFrame frame = new JFrame();
    final JFileChooser fc = new JFileChooser(selectFile);
    fc.setDialogType(JFileChooser.OPEN_DIALOG);
    fc.setMultiSelectionEnabled(true);
    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fc.setDialogTitle("Add File or Directory");
    final int option = fc.showOpenDialog(frame);

    if (option == JFileChooser.APPROVE_OPTION) {
        final File[] files = fc.getSelectedFiles();
        final String message = ApplicationContextUtil.getMessage("bag.message.filesadded");
        if (files != null && files.length > 0) {
            addBagData(files);
            ApplicationContextUtil.addConsoleMessage(message + " " + getFileNames(files));
        } else {
            final File file = fc.getSelectedFile();
            addBagData(file);
            ApplicationContextUtil.addConsoleMessage(message + " " + file.getAbsolutePath());
        }
        bagView.bagPayloadTreePanel.refresh(bagView.bagPayloadTree);
        bagView.updateAddData();
    }
}

From source file:org.spf4j.ui.TSDBViewJInternalFrame.java

@edu.umd.cs.findbugs.annotations.SuppressWarnings("UP_UNUSED_PARAMETER")
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    TreePath[] selectionPaths = measurementTree.getSelectionPaths();
    List<String> selectedTables = getSelectedTables(selectionPaths);
    if (!selectedTables.isEmpty()) {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            try {
                tsDb.writeCsvTables(selectedTables, file);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }//from  ww w.  ja  v  a 2s . c o m
        }
    }
}

From source file:filesscanner.MainWindow.java

private void saveToJSONBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveToJSONBtnActionPerformed

    JSONObject object = new JSONObject();

    for (int i = 0; i < modelFiles.getSize(); i++) {

        object.put("filename" + i, modelFiles.get(i).toString());

    }//from  w ww. j a va  2s  .c om

    File file = new File("C:");
    JFileChooser chooser = ShowChooser(file);
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setDialogType(JFileChooser.SAVE_DIALOG);
    int res = chooser.showDialog(this, "  ? ??!!!");
    if (res == JFileChooser.APPROVE_OPTION) {

        try {
            Writer writer = new FileWriter(chooser.getSelectedFile());
            object.writeJSONString(writer);
            JOptionPane.showMessageDialog(rootPane, "SAVED SUCCESFULLY!!!");
            writer.flush();
            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(rootPane, "Something bad!!!");
        }

    }

}