Example usage for javax.swing JFileChooser setApproveButtonText

List of usage examples for javax.swing JFileChooser setApproveButtonText

Introduction

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

Prototype


@BeanProperty(preferred = true, description = "The text that goes in the ApproveButton.")
public void setApproveButtonText(String approveButtonText) 

Source Link

Document

Sets the text used in the ApproveButton in the FileChooserUI.

Usage

From source file:Main.java

public static void main(String[] argv) {
    JFileChooser chooser = new JFileChooser();
    // Set the text
    chooser.setApproveButtonText("New Approve Text");
    // Set the mnemonic
    chooser.setApproveButtonMnemonic('a');
    // Set the tool tip
    chooser.setApproveButtonToolTipText("New Approve Tool Tip");
    chooser.showOpenDialog(null);//from   w w  w  .  j a v a  2s  . c om
}

From source file:Main.java

public static void main(String[] argv) {
    JFileChooser chooser = new JFileChooser();
    // Set the text
    chooser.setApproveButtonText("New Approve Text");
    // Set the mnemonic
    chooser.setApproveButtonMnemonic((int) 'a');
    // Set the tool tip
    chooser.setApproveButtonToolTipText("New Approve Tool Tip");
    chooser.showOpenDialog(null);/*from  www  .  jav a  2 s .  c o m*/
}

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.//w  w  w  . j ava  2s.  c  o m
 * @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:Main.java

public static File chooseDir(Component parent, String title) {
    JFileChooser chooser = new JFileChooser();
    // chooser.setCurrentDirectory(new java.io.File("."));
    chooser.setDialogTitle(title);// w  w  w.  j  a v  a 2  s  . c  om
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setApproveButtonText("Select Dir");
    return chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION ? chooser.getSelectedFile() : null;
}

From source file:Main.java

public static String browseForFile(Window owner, String file, int selectionMode, String title,
        FileFilter filter) {/*from w w  w.  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: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 w w.j  av  a 2  s  .  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:SwingUtil.java

/**
 * Get a file selection using the FileChooser dialog.
 *
 * @param owner/*  w w w  . j  a v  a 2  s  . c om*/
 * The parent of this modal dialog.
 * @param defaultSelection
 * The default file selection as a file.
 * @param filter
 * An extension filter
 * @param title
 * The caption for the dialog.
 *
 * @return
 * A selected file or null if no selection is made.
 */
public static File getFileChoice(Component owner, File defaultSelection, FileFilter filter, String title) {
    //
    // There is apparently a bug in the native Windows FileSystem class that
    // occurs when you use a file chooser and there is a security manager
    // active. An error dialog is displayed indicating there is no disk in
    // Drive A:. To avoid this, the security manager is temporarily set to
    // null and then reset after the file chooser is closed.
    //
    SecurityManager sm = null;
    File choice = null;
    JFileChooser chooser = null;

    sm = System.getSecurityManager();
    System.setSecurityManager(null);

    chooser = new JFileChooser();
    if (defaultSelection.isDirectory()) {
        chooser.setCurrentDirectory(defaultSelection);
    } else {
        chooser.setSelectedFile(defaultSelection);
    }
    chooser.setFileFilter(filter);
    chooser.setDialogTitle(title);
    chooser.setApproveButtonText("OK");
    int v = chooser.showOpenDialog(owner);

    owner.requestFocus();
    switch (v) {
    case JFileChooser.APPROVE_OPTION:
        if (chooser.getSelectedFile() != null) {
            choice = chooser.getSelectedFile();
        }
        break;
    case JFileChooser.CANCEL_OPTION:
    case JFileChooser.ERROR_OPTION:
    }
    chooser.removeAll();
    chooser = null;
    System.setSecurityManager(sm);
    return choice;
}

From source file:SwingUtil.java

/**
 * Open a JFileChooser dialog for selecting a directory and return the
 * selected directory./*ww w .  j  a va 2 s  .  c  o  m*/
 *
 *
 * @param owner
 * The frame or dialog that controls the invokation of this dialog.
 * @param defaultDir
 * The directory to show when the dialog opens.
 * @param title
 * Tile for the dialog.
 *
 * @return
 * The selected directory as a File. Null if user cancels dialog without
 * a selection.
 *
 */
public static File getDirectoryChoice(Component owner, File defaultDir, String title) {
    //
    // There is apparently a bug in the native Windows FileSystem class that
    // occurs when you use a file chooser and there is a security manager
    // active. An error dialog is displayed indicating there is no disk in
    // Drive A:. To avoid this, the security manager is temporarily set to
    // null and then reset after the file chooser is closed.
    //
    SecurityManager sm = null;
    JFileChooser chooser = null;
    File choice = null;

    sm = System.getSecurityManager();
    System.setSecurityManager(null);
    chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    if ((defaultDir != null) && defaultDir.exists() && defaultDir.isDirectory()) {
        chooser.setCurrentDirectory(defaultDir);
        chooser.setSelectedFile(defaultDir);
    }
    chooser.setDialogTitle(title);
    chooser.setApproveButtonText("OK");
    int v = chooser.showOpenDialog(owner);

    owner.requestFocus();
    switch (v) {
    case JFileChooser.APPROVE_OPTION:
        if (chooser.getSelectedFile() != null) {
            if (chooser.getSelectedFile().exists()) {
                choice = chooser.getSelectedFile();
            } else {
                File parentFile = new File(chooser.getSelectedFile().getParent());

                choice = parentFile;
            }
        }
        break;
    case JFileChooser.CANCEL_OPTION:
    case JFileChooser.ERROR_OPTION:
    }
    chooser.removeAll();
    chooser = null;
    System.setSecurityManager(sm);
    return choice;
}

From source file:com.nubits.nubot.launch.toolkit.LaunchUI.java

private JFileChooser createFileChoser() {
    JFileChooser fileChooser = new JFileChooser();

    // Set the text of the button
    fileChooser.setApproveButtonText("Import");
    // Set the tool tip
    fileChooser.setApproveButtonToolTipText("Import configuration file");

    //Filter .json files
    FileFilter filter = new FileNameExtensionFilter(".json files", "json");
    fileChooser.setFileFilter(filter);/* ww  w .  j a v  a  2 s  . c o m*/

    fileChooser.setCurrentDirectory(new File(local_path));
    return fileChooser;
}

From source file:org.pgptool.gui.ui.tools.browsefs.SaveFileChooserDialog.java

private JFileChooser prepareFileChooser() {
    JFileChooser ofd = new JFileChooser();
    ofd.setFileSelectionMode(JFileChooser.FILES_ONLY);
    ofd.setMultiSelectionEnabled(false);
    ofd.setDialogTitle(Messages.get(dialogTitleCode));
    ofd.setApproveButtonText(Messages.get(approvalButtonTextCode));
    onFileChooserPostConstrct(ofd);/*from   ww  w .j ava2 s .  c  o m*/
    suggestTarget(ofd);
    return ofd;
}