Example usage for javax.swing JFileChooser setSelectedFile

List of usage examples for javax.swing JFileChooser setSelectedFile

Introduction

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

Prototype

@BeanProperty(preferred = true)
public void setSelectedFile(File file) 

Source Link

Document

Sets the selected file.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JButton button = new JButton("Click me");
    button.addActionListener(e -> {//w  w  w.j a va 2 s  .c  o m
        JFileChooser chooser = new JFileChooser();
        chooser.setSelectedFile(new File(chooser.getCurrentDirectory(), "save.dat"));
        final JTextField textField = getTexField(chooser);
        if (textField == null) {
            return;
        }
        String text = textField.getText();
        if (text == null) {
            return;
        }
        int index = text.lastIndexOf('.');
        if (index == -1) {
            return;
        }
        textField.setSelectionStart(0);
        textField.setSelectionEnd(index);

        chooser.showSaveDialog(button);

    });
    frame.add(button);
    frame.pack();
    frame.setVisible(true);
}

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.setSelectedFile(f);
    chooser.showOpenDialog(null);/*from   w w  w  .  ja  v  a2s .  com*/
    File curFile = chooser.getSelectedFile();
}

From source file:Main.java

public static void main(String[] args) {
    final JFileChooser chooser = new JFileChooser(new File(".")) {
        public void approveSelection() {
            if (getSelectedFile().exists()) {
                super.approveSelection();
            } else
                System.out.println("File doesn't exist");
        }/*w  w w .  j a v  a  2s.c om*/
    };

    chooser.addActionListener(e -> System.out.println(e));

    chooser.setSelectedFile(new File("something.txt"));
    int returnVal = chooser.showSaveDialog(null);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        System.out.println(chooser.getSelectedFile());
    }

}

From source file:Main.java

/**
 * Set starting dir and file for a chooser.
 * chooser.setSelectedFile doesn't work correctly when given a directory.
 *
 * @param chooser/*from w  ww  . j  a v a 2s.  c om*/
 * @param file
 */
public static void setSelectedFile(JFileChooser chooser, File file) {
    if (file.isDirectory())
        chooser.setCurrentDirectory(file);
    else
        chooser.setSelectedFile(file);
}

From source file:grafix.principal.Comandos.java

static public void cmdSalvarJPEG() {
    ControleRegistro.alertaRegistro();/* w  w w . j a  v a 2  s  . com*/
    JFileChooser chooser = new JFileChooser();
    chooser.setSelectedFile(new File(".jpg"));
    int returnVal = chooser.showSaveDialog(Controle.getTela());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        try {
            ChartUtilities.saveChartAsJPEG(file, Controle.getJanelaAtiva().getPanelGraficos().getChart(),
                    Controle.getJanelaAtiva().getWidth(), Controle.getJanelaAtiva().getHeight());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:com.floreantpos.bo.actions.DataExportAction.java

public static JFileChooser getFileChooser() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fileChooser.setMultiSelectionEnabled(false);
    fileChooser.setSelectedFile(new File("floreantpos-menu-items.xml")); //$NON-NLS-1$
    fileChooser.setFileFilter(new FileFilter() {

        @Override//from   ww  w  . ja  v a 2 s. c om
        public String getDescription() {
            return "XML File"; //$NON-NLS-1$
        }

        @Override
        public boolean accept(File f) {
            if (f.getName().endsWith(".xml")) { //$NON-NLS-1$
                return true;
            }

            return false;
        }
    });
    return fileChooser;
}

From source file:grafix.principal.Comandos.java

static public void cmdSalvarConfiguracao() {
    ControleRegistro.alertaRegistro();/*from ww  w  . j  av  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:SwingUtil.java

/**
 * Get a file selection using the FileChooser dialog.
 *
 * @param owner//from w  w w.  j av a  2 s. c o m
 * 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.//from   w ww.j  av a 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:Main.java

public static String browseForFile(Window owner, String file, int selectionMode, String title,
        FileFilter filter) {/*  w w w.j  a  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;
}