Example usage for javax.swing JFileChooser APPROVE_OPTION

List of usage examples for javax.swing JFileChooser APPROVE_OPTION

Introduction

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

Prototype

int APPROVE_OPTION

To view the source code for javax.swing JFileChooser APPROVE_OPTION.

Click Source Link

Document

Return value if approve (yes, ok) is chosen.

Usage

From source file:Main.java

/**
 * Consistent way to chosing multiple files to open with JFileChooser.
 * <p>/*from   w  w  w . j av  a2 s  .co m*/
 * 
 * @see JFileChooser#setFileSelectionMode(int)
 * @see #getSystemFiles(Component, int)
 * @param owner to show the component relative to.
 * @param mode selection mode for the JFileChooser.
 * @return File[] based on the user selection can be null.
 */
public static File[] getSystemFiles(Component owner, int mode, FileFilter[] filters) {

    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(mode);
    jfc.setFileHidingEnabled(true);
    jfc.setMultiSelectionEnabled(true);
    jfc.setAcceptAllFileFilterUsed(true);
    if (filters != null) {
        for (int i = 0; i < filters.length; i++) {
            jfc.addChoosableFileFilter(filters[i]);
        }

        if (filters.length >= 1) {
            jfc.setFileFilter(filters[0]);
        }
    }

    int result = jfc.showOpenDialog(owner);
    if (result == JFileChooser.APPROVE_OPTION) {
        return jfc.getSelectedFiles();
    }

    return new File[0];
}

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

public static void installServer(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 fabricVersion = attributes.getValue("FabricVersion");
        jarFile.close();/*  w  w w .j av a  2s .  c  om*/
        File fabricJar = new File(mcDir, "fabric-" + fabricVersion + ".jar");
        if (fabricJar.exists()) {
            fabricJar.delete();
        }
        FileUtils.copyFile(inputFile, fabricJar);
        ServerInstaller.install(mcDir, fabricVersion, progress, fabricJar);
    } else {
        throw new Exception("Failed to find jar");
    }

}

From source file:SimpleFileChooser.java

public SimpleFileChooser() {
    super("File Chooser Test Frame");
    setSize(350, 200);//from  ww w.j  av  a 2s . c  o  m
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container c = getContentPane();
    c.setLayout(new FlowLayout());

    JButton openButton = new JButton("Open");
    JButton saveButton = new JButton("Save");
    JButton dirButton = new JButton("Pick Dir");
    final JLabel statusbar = new JLabel("Output of your selection will go here");

    // Create a file chooser that opens up as an Open dialog
    openButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(true);
            int option = chooser.showOpenDialog(SimpleFileChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                File[] sf = chooser.getSelectedFiles();
                String filelist = "nothing";
                if (sf.length > 0)
                    filelist = sf[0].getName();
                for (int i = 1; i < sf.length; i++) {
                    filelist += ", " + sf[i].getName();
                }
                statusbar.setText("You chose " + filelist);
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

    // Create a file chooser that opens up as a Save dialog
    saveButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(SimpleFileChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                statusbar.setText("You saved "
                        + ((chooser.getSelectedFile() != null) ? chooser.getSelectedFile().getName()
                                : "nothing"));
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

    // Create a file chooser that allows you to pick a directory
    // rather than a file
    dirButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int option = chooser.showOpenDialog(SimpleFileChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                statusbar.setText("You opened "
                        + ((chooser.getSelectedFile() != null) ? chooser.getSelectedFile().getName()
                                : "nothing"));
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

    c.add(openButton);
    c.add(saveButton);
    c.add(dirButton);
    c.add(statusbar);
}

From source file:Main.java

public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    if (source == openItem) {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("."));
        chooser.setFileFilter(new FileFilter() {
            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".zip") || f.isDirectory();
            }/*from  w  ww  .  j  a va2  s  .co m*/

            public String getDescription() {
                return "ZIP Files";
            }
        });
        int r = chooser.showOpenDialog(this);
        if (r == JFileChooser.APPROVE_OPTION) {
            String zipname = chooser.getSelectedFile().getPath();
            System.out.println(zipname);
        }
    } else if (source == exitItem)
        System.exit(0);
}

From source file:ImageFileFilterImageScale.java

private String getImageFile() {
    JFileChooser fc = new JFileChooser();
    fc.setFileFilter(new ImageFilter());
    int result = fc.showOpenDialog(null);
    File file = null;//  ww w  .  ja v  a  2 s .c  o m
    if (result == JFileChooser.APPROVE_OPTION) {
        file = fc.getSelectedFile();
        return file.getPath();
    } else
        return null;

}

From source file:com.intuit.tank.tools.debugger.SaveTextAction.java

/**
 * /*from   w w w .  ja  v a2 s . c  o  m*/
 */
protected void showSaveDialog() {
    int response = saveAsChooser.showSaveDialog(parent);
    if (response == JFileChooser.APPROVE_OPTION) {
        File selectedFile = saveAsChooser.getSelectedFile();
        if (selectedFile.exists()) {
            int confirm = JOptionPane.showConfirmDialog(parent,
                    "Overwrite file " + selectedFile.getName() + "?");
            if (confirm != JOptionPane.YES_OPTION) {
                return;
            }
        }
        FileWriter fw = null;
        try {
            fw = new FileWriter(selectedFile);
            writer.writeText(fw);
        } catch (Exception e) {
            System.err.println("Error writing file: " + e.toString());
            e.printStackTrace();
            JOptionPane.showMessageDialog(parent, "Error writing file: " + e.toString(), "Error",
                    JOptionPane.ERROR_MESSAGE);
        } finally {
            IOUtils.closeQuietly(fw);
        }
    }

}

From source file:ImageViewer.java

public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    if (source == openItem) {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("."));

        chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".gif") || f.isDirectory();
            }/*ww w .  j  a  v a  2s .  c  o m*/

            public String getDescription() {
                return "GIF Images";
            }
        });

        int r = chooser.showOpenDialog(this);
        if (r == JFileChooser.APPROVE_OPTION) {
            String name = chooser.getSelectedFile().getName();
            label.setIcon(new ImageIcon(name));
        }
    } else if (source == exitItem)
        System.exit(0);
}

From source file:com.googlecode.bpmn_simulator.gui.dialogs.ImageExportChooser.java

public boolean showExportDialog(final Component parent) {
    return showSaveDialog(parent) == JFileChooser.APPROVE_OPTION;
}

From source file:MyFilterChooser.java

public MyFilterChooser() {
    super("Filter Test Frame");
    setSize(350, 200);//from   w w w .  j  a v a2  s.c  o  m
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container c = getContentPane();
    c.setLayout(new FlowLayout());

    JButton openButton = new JButton("Open");
    final JLabel statusbar = new JLabel("Output of your selection will go here");

    openButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            String[] pics = new String[] { "gif", "jpg", "tif" };
            String[] audios = new String[] { "au", "aiff", "wav" };
            JFileChooser chooser = new JFileChooser();
            chooser.addChoosableFileFilter(new SimpleFileFilter(pics, "Images (*.gif, *.jpg, *.tif)"));
            chooser.addChoosableFileFilter(new SimpleFileFilter(".MOV"));
            chooser.addChoosableFileFilter(new SimpleFileFilter(audios, "Sounds (*.aiff, *.au, *.wav)"));
            int option = chooser.showOpenDialog(MyFilterChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                if (chooser.getSelectedFile() != null)
                    statusbar.setText("You chose " + chooser.getSelectedFile().getName());
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

    c.add(openButton);
    c.add(statusbar);
    setVisible(true);
}

From source file:de.dakror.virtualhub.server.dialog.BackupEditDialog.java

public static void show() throws JSONException {
    final JDialog dialog = new JDialog(Server.currentServer.frame, "Backup-Einstellungen", true);
    dialog.setSize(400, 250);/*from   w w w.ja  v  a2  s  .com*/
    dialog.setLocationRelativeTo(Server.currentServer.frame);
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

    JPanel cp = new JPanel(new SpringLayout());
    cp.add(new JLabel("Zielverzeichnis:"));
    JPanel panel = new JPanel();
    final JTextField path = new JTextField((Server.currentServer.settings.has("backup.path")
            ? Server.currentServer.settings.getString("backup.path")
            : ""), 10);
    panel.add(path);
    panel.add(new JButton(new AbstractAction("Whlen...") {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser jfc = new JFileChooser((path.getText().length() > 0 ? new File(path.getText())
                    : new File(System.getProperty("user.home"))));
            jfc.setFileHidingEnabled(false);
            jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            jfc.setDialogTitle("Backup-Zielverzeichnis whlen");

            if (jfc.showOpenDialog(dialog) == JFileChooser.APPROVE_OPTION)
                path.setText(jfc.getSelectedFile().getPath().replace("\\", "/"));
        }
    }));

    cp.add(panel);

    cp.add(new JLabel(""));
    cp.add(new JLabel(""));

    cp.add(new JButton(new AbstractAction("Abbrechen") {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            dialog.dispose();
        }
    }));
    cp.add(new JButton(new AbstractAction("Speichern") {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if (path.getText().length() > 0)
                    Server.currentServer.settings.put("backup.path", path.getText());
                dialog.dispose();
            } catch (JSONException e1) {
                e1.printStackTrace();
            }
        }
    }));

    SpringUtilities.makeCompactGrid(cp, 3, 2, 6, 6, 6, 6);
    dialog.setContentPane(cp);
    dialog.pack();
    dialog.setVisible(true);
}