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:net.sf.vntconverter.VntConverter.java

/**
 * Fhrt den VntConverter mit den angegebnen Optionen aus.
 *//*  w  w  w .  j  a  v  a  2  s. co  m*/
public static void main(String[] args) {
    try {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            // Wenn nicht, dann nicht ...
        }
        JFileChooser fileChooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Text files (txt) and memo files (vnt)",
                "txt", "vnt");
        fileChooser.setCurrentDirectory(new java.io.File("."));
        fileChooser.setDialogTitle("Choose files to convert ...");
        fileChooser.setFileFilter(filter);
        fileChooser.setMultiSelectionEnabled(true);
        if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            JFileChooser directoryChooser = new JFileChooser();
            directoryChooser.setCurrentDirectory(new java.io.File("."));
            directoryChooser.setDialogTitle("Choose target directory ...");
            directoryChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            directoryChooser.setAcceptAllFileFilterUsed(false);
            if (directoryChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                VntConverter converter = new VntConverter();
                String targetDirectoy = directoryChooser.getSelectedFile().getAbsolutePath();
                System.out.println(targetDirectoy);
                for (File file : fileChooser.getSelectedFiles()) {
                    if (file.getName().endsWith(".txt")) {
                        converter.encode(file,
                                new File(targetDirectoy + "\\" + file.getName().replace(".txt", ".vnt")));
                    } else if (file.getName().endsWith(".vnt")) {
                        converter.decode(file,
                                new File(targetDirectoy + "\\" + file.getName().replace(".vnt", ".txt")));
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Exception caught", e);
    }
}

From source file:jsonbrowse.JsonBrowse.java

/**
 * @param args the command line arguments
 *//*  w  w  w  .  java  2s  .  c om*/
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
            | javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(JsonBrowse.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    }
    //</editor-fold>

    String fileName;

    // Get name of JSON file
    Path jsonFilePath;
    if (args.length < 1) {

        JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
        fc.setDialogTitle("Select JSON file...");
        fc.setFileFilter(new FileNameExtensionFilter("JSON files (*.json/*.txt)", "json", "txt"));
        if ((fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)) {
            jsonFilePath = fc.getSelectedFile().toPath().toAbsolutePath();
        } else {
            return;
        }
    } else {
        Path path = Paths.get(args[0]);
        if (!path.isAbsolute())
            jsonFilePath = Paths.get(System.getProperty("user.dir")).resolve(path);
        else
            jsonFilePath = path;
    }

    // Run app

    try {
        JsonBrowse app = new JsonBrowse(jsonFilePath);
        app.setVisible(true);
    } catch (FileNotFoundException ex) {
        System.out.println("Input file '" + jsonFilePath + "' not found.");
        System.exit(1);
    } catch (IOException ex) {
        System.out.println("Error reading from file '" + jsonFilePath + "'.");
        System.exit(1);
    } catch (InterruptedException ex) {
        Logger.getLogger(JsonBrowse.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:Main.java

public static File promptForImageFile(JPanel parent) {
    JFileChooser chooser = new JFileChooser();
    int returnVal = chooser.showOpenDialog(parent);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
        return chooser.getSelectedFile();
    }//from  ww  w  . ja  va  2s .  c om
    return null;
}

From source file:Main.java

public static int showFileChooserLoop(Component parentComponent, JFileChooser chooser) {
    for (;;) {//  www .ja va 2  s. c om
        int option = chooser.showOpenDialog(parentComponent);
        if (option != JFileChooser.APPROVE_OPTION) {
            return JOptionPane.CANCEL_OPTION;
        }

        File file = chooser.getSelectedFile();
        if (file.exists()) {
            int op2 = showReplaceExistingFileConfirmDialog(parentComponent, file);
            if (op2 == JOptionPane.YES_OPTION) {
                return JOptionPane.YES_OPTION;
            } else if (op2 == JOptionPane.CANCEL_OPTION) {
                return JOptionPane.CANCEL_OPTION;
            }
        } else {
            return JOptionPane.YES_OPTION;
        }
    }
}

From source file:com.google.code.facebook.graph.sna.applet.GraphEditorDemo.java

/**
 * a driver for this demo//from   ww w . ja v 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();
                }
            }
        }
    });
    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:Main.java

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

From source file:Main.java

/**
 * /*from w  ww . j  a v a  2 s .  co m*/
 * @param owner
 * @return El directorio seleccionado
 */
public static File saveFileChooser(Window owner) {
    int userResponse;
    JFileChooser fileChooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("XML", "xml");
    fileChooser.setFileFilter(filter);
    userResponse = fileChooser.showSaveDialog(owner);
    if (JFileChooser.APPROVE_OPTION == userResponse) {
        return fileChooser.getSelectedFile();
    }
    return null;
}

From source file:external.jung.demo.GraphEditorDemo.java

/**
 * a driver for this demo//w ww. j  a  v  a  2 s .  co m
 */
@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();
                }
            }
        }
    });
    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: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 w  w . ja v  a  2s .  c om
public static File chooseDirectory(String title, String startDirectory) {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setDialogTitle(title);

    if (startDirectory != null && !startDirectory.trim().equals("")) {
        chooser.setCurrentDirectory(new File(startDirectory));
    }

    int status = chooser.showOpenDialog(null);

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

    return null;
}

From source file:Main.java

/**
 * Opens a file chooser dialog//from www  .  j a v  a2 s .c  o  m
 * @param chooseAFolder true if the dialog should return a folder, false for a file
 */
public static String chooseFile(JComboBox comboBox, boolean chooseAFolder) {
    JFileChooser chooser = new JFileChooser();
    String path = "";
    String currentDir = "";

    if (comboBox.getSelectedItem() != null) {
        currentDir = comboBox.getSelectedItem().toString();
    }

    // Set whether we want to select a folder instead of a file
    if (chooseAFolder) {
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    }

    chooser.setCurrentDirectory(new File(currentDir));

    // Show open dialog; this method does not return until the dialog is closed
    if (chooser.showOpenDialog(comboBox) == JFileChooser.APPROVE_OPTION) {
        path = chooser.getSelectedFile().getAbsolutePath();
    }

    if (path == null || path.equals("")) {
        return currentDir;
    }
    return path;
}