Example usage for javax.swing JFileChooser JFileChooser

List of usage examples for javax.swing JFileChooser JFileChooser

Introduction

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

Prototype

public JFileChooser() 

Source Link

Document

Constructs a JFileChooser pointing to the user's default directory.

Usage

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"./*ww w  . j a va  2  s  .c om*/
 * @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:ExcelComponents.FileOpener.java

public static File openfile() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setAcceptAllFileFilterUsed(false);
    fileChooser.addChoosableFileFilter(excelfilter);

    int returnValue = fileChooser.showOpenDialog(null);
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        return (selectedFile);
    }/*from   w  ww . j a  va2 s  . c o  m*/
    return null;

}

From source file:Main.java

/**
 * Ask the user of a directory./*from w  w w  . ja  v a2s  .c o m*/
 *
 * @return The directory chosen by the user or null if the user has canceled the view.
 */
public static File chooseDirectory() {
    if (chooser == null) {
        chooser = new JFileChooser();
    }

    chooser.setAcceptAllFileFilterUsed(false);
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    int returnCode = chooser.showOpenDialog(new JFrame());

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

    return null;
}

From source file:MainClass.java

public MainClass() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setDialogTitle("Choose a file");
    this.getContentPane().add(fileChooser);
    fileChooser.setVisible(true);/*from  w  w w .  j  a v  a  2s.c om*/
}

From source file:Main.java

/**
 * Gets a text file chooser. Only files with the extension ".txt" and directories are shown.
 *
 * @return the file chooser/*from w ww.j  a v  a 2 s  .  c  om*/
 */
public static JFileChooser getTextFileChooser() {
    final JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Text files", "txt");
    chooser.setFileFilter(filter);
    return chooser;
}

From source file:net.fabricmc.installer.installer.LocalVersionInstaller.java

public static void install(File mcDir, IInstallerProgress progress) throws Exception {
    JFileChooser fc = new JFileChooser();
    fc.setDialogTitle(Translator.getString("install.client.selectCustomJar"));
    fc.setFileFilter(new FileNameExtensionFilter("Jar Files", "jar"));
    if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        File inputFile = fc.getSelectedFile();

        JarFile jarFile = new JarFile(inputFile);
        Attributes attributes = jarFile.getManifest().getMainAttributes();
        String mcVersion = attributes.getValue("MinecraftVersion");
        Optional<String> stringOptional = ClientInstaller.isValidInstallLocation(mcDir, mcVersion);
        jarFile.close();//from  w ww. ja  v a  2  s  .com
        if (stringOptional.isPresent()) {
            throw new Exception(stringOptional.get());
        }
        ClientInstaller.install(mcDir, mcVersion, progress, inputFile);
    } else {
        throw new Exception("Failed to find jar");
    }

}

From source file:com.willwinder.universalgcodesender.uielements.components.FirmwareSettingsFileTypeFilter.java

public static JFileChooser getSettingsFileChooser() {
    FirmwareSettingsFileTypeFilter filter = new FirmwareSettingsFileTypeFilter();

    // Setup file browser with the last path used.
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fileChooser.setFileHidingEnabled(true);
    fileChooser.addChoosableFileFilter(filter);
    fileChooser.setAcceptAllFileFilterUsed(true);
    fileChooser.setFileFilter(filter);/*from  ww  w  . j a  va 2 s . c  om*/

    return fileChooser;
}

From source file:GraphEditorDemo.java

/**
 * a driver for this demo//from  w w  w.j av  a 2  s.  c  om
 */
@SuppressWarnings("serial")
public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final GraphEditorDemo demo = new GraphEditorDemo();

    JMenu menu = new JMenu("File");
    menu.add(new AbstractAction("Make Image") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                demo.writeJPEGImage(file);
            }
        }
    });
    menu.add(new AbstractAction("Print") {
        public void actionPerformed(ActionEvent e) {
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setPrintable(demo);
            if (printJob.printDialog()) {
                try {
                    printJob.print();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    menu.add(new AbstractAction("Save") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                demo.writeTopologyFile(file);
            }
        }
    });
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);
    frame.getContentPane().add(demo);
    frame.pack();
    frame.setVisible(true);
}

From source file:ExcelComponents.FileOpener.java

public static File[] openfiles() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setAcceptAllFileFilterUsed(false);
    fileChooser.addChoosableFileFilter(excelfilter);
    fileChooser.setMultiSelectionEnabled(true);
    int returnValue = fileChooser.showOpenDialog(null);
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        File[] selectedFiles = fileChooser.getSelectedFiles().clone();
        return (selectedFiles);
    }/*from  w w w. java 2  s. c o  m*/
    return null;

}

From source file:Main.java

/**
 * Displays a {@link JFileChooser} to select a directory.
 *
 * @param title The title of the dialog.
 * @param startDirectory The directory where the dialog is initialed opened.
 * @return The {@link File} selected, returns null if no directory was selected.
 *//*from w  ww  . j  a v a 2 s .c  o m*/
public static File chooseDirectory(String title, File startDirectory) {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setDialogTitle(title);

    if (startDirectory != null) {
        chooser.setCurrentDirectory(startDirectory);
    }

    int status = chooser.showOpenDialog(null);

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

    return null;
}