Example usage for javax.swing JFileChooser showOpenDialog

List of usage examples for javax.swing JFileChooser showOpenDialog

Introduction

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

Prototype

public int showOpenDialog(Component parent) throws HeadlessException 

Source Link

Document

Pops up an "Open File" file chooser dialog.

Usage

From source file:iotest.ModifyArtifactsTest.java

@Test
public void modifyAppServletXml()
        throws UnsupportedEncodingException, FileNotFoundException, IOException, JDOMException {

    System.out.println("please select app-servlet.xml file ");
    JFileChooser fc = new JFileChooser();
    int retValue = fc.showOpenDialog(new JPanel());
    if (retValue == JFileChooser.APPROVE_OPTION) {
        File f = fc.getSelectedFile();

        SAXBuilder builder = new SAXBuilder();

        Document document = (Document) builder.build(f);
        Element rootNode = document.getRootElement();

        List<Element> list = rootNode.getChildren("component-scan",
                Namespace.getNamespace("http://www.springframework.org/schema/context"));

        Element e = list.get(0);//w  w  w  .ja  va 2  s .  co m

        String cnt = "<context:exclude-filter type=\"regex\" expression=\"pegasus\\.module\\.jfilelooder\\..*\" />";

        e.addContent(cnt);

        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()) {
            @Override
            public String escapeElementEntities(String str) {
                return str;
            }
        };

        Writer writer = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
        outputter.output(document, writer);
        writer.close();
    } else {
        System.out.println("Next time select a file.");
        System.exit(1);
    }

}

From source file:de.jcup.egradle.other.GroovyParserSourceCollector.java

private void start(String[] args) throws IOException {
    File groovyProjectDirectory;/*from  ww  w .  j ava  2  s.  c o  m*/
    if (args.length == 0) {
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fc.showOpenDialog(null);
        groovyProjectDirectory = fc.getSelectedFile();
    } else {
        groovyProjectDirectory = new File(args[0]);
    }
    if (groovyProjectDirectory == null) {
        System.err.println("canceled");
        System.exit(1);
    }
    System.out.println("use groovy sources at:" + groovyProjectDirectory.getAbsolutePath());

    assertDirectoryExists(groovyProjectDirectory);
    File groovyTempDirectory = new File(groovyProjectDirectory, "/target/tmp/groovydoc");
    File target = new File("./..//egradle-plugin-main/src/main/java-groovy");
    assertDirectoryExists(target);
    execute(groovyTempDirectory, target);
    System.out.println("DONE");
}

From source file:SimpleFileChooser.java

public SimpleFileChooser() {
    super("File Chooser Test Frame");
    setSize(350, 200);/*from ww  w.java 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:eu.apenet.dpt.standalone.gui.XsltAdderActionListener.java

public void actionPerformed(ActionEvent e) {
    JFileChooser xsltChooser = new JFileChooser();
    xsltChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

    if (xsltChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
        File file = xsltChooser.getSelectedFile();
        if (isXSLT(file)) {
            if (saveXslt(file)) {
                JRadioButton newButton = new JRadioButton(file.getName());
                newButton.addActionListener(new XsltSelectorListener(dataPreparationToolGUI));
                dataPreparationToolGUI.getGroupXslt().add(newButton);
                dataPreparationToolGUI.getAPEPanel().getApeTabbedPane().addToXsltPanel(newButton);
                dataPreparationToolGUI.getAPEPanel().getApeTabbedPane()
                        .addToXsltPanel(Box.createRigidArea(new Dimension(0, 10)));
                JOptionPane.showMessageDialog(parent, labels.getString("xsltSaved") + ".",
                        labels.getString("fileSaved"), JOptionPane.INFORMATION_MESSAGE, Utilities.icon);
            } else {
                JOptionPane.showMessageDialog(parent, labels.getString("xsltNotSaved") + ".",
                        labels.getString("fileNotSaved"), JOptionPane.ERROR_MESSAGE, Utilities.icon);
            }/*from   w ww. j a va  2 s. co m*/
        } else {
            JOptionPane.showMessageDialog(parent, labels.getString("xsltNotSaved") + ".",
                    labels.getString("fileNotSaved"), JOptionPane.ERROR_MESSAGE, Utilities.icon);
        }
    }
}

From source file:it.unibas.spicygui.controllo.preprocessing.ActionChangeDelimiter.java

@Override
public void performAction() {
    JFileChooser chooser = vista.getFileChooserApriCSV();
    File file;//from   w  w w .ja  v  a  2s  .  c  o  m
    int returnVal = chooser.showOpenDialog(WindowManager.getDefault().getMainWindow());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        //file extension
        String ext = file.getPath().substring(file.getPath().lastIndexOf(".") + 1);
        if (ext.equalsIgnoreCase("csv")) {
            ChangeDelimiterFrame frame = new ChangeDelimiterFrame();
            String sourceDelimiter = frame.getSourceDelimiter();
            String sourceQuotes = frame.getSourceQuotes();
            boolean useTargetQuotes = frame.getTargetQuotes();
            //                System.out.println(sourceDelimiter);
            //                System.out.println(sourceQuotes);
            //                System.out.println(useTargetQuotes);
            if (sourceDelimiter != null && sourceQuotes != null)
                try {
                    ChangeDelimiterCSV changer = new ChangeDelimiterCSV();
                    changer.changeDelimiter(file, sourceDelimiter, sourceQuotes, useTargetQuotes);
                    DialogDisplayer.getDefault()
                            .notify(new NotifyDescriptor.Message(
                                    NbBundle.getMessage(Costanti.class, Costanti.EXPORT_CHANGED_DELIMITER)
                                            + file.getParent()));
                } catch (IOException ex) {
                    DialogDisplayer.getDefault().notify(
                            new NotifyDescriptor.Message(ex.getMessage(), DialogDescriptor.ERROR_MESSAGE));
                }
        } else {
            DialogDisplayer.getDefault()
                    .notify(new NotifyDescriptor.Message(
                            NbBundle.getMessage(Costanti.class, Costanti.MESSAGE_WRONG_FILE_INPUT) + ": \""
                                    + ext + "\"",
                            DialogDescriptor.ERROR_MESSAGE));
        }
    }
}

From source file:presenter.MainPresenter.java

@Override
public void loadMovementsequenceFromFile(ActionEvent e) {
    JFileChooser fc = new JFileChooser();

    if (fc.showOpenDialog((Component) e.getSource()) == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        try {/*from w ww . jav a 2  s.c o m*/
            this.movSeq = new Movementsequence(new Scanner(file).useDelimiter("\\A").next());
            this.emissionsequenceModel = new EmissionsequenceModel(this.movSeq);
            this.displayStatus("File was read successfully!");
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}

From source file:presenter.MainPresenter.java

@Override
public void loadEmissionsequenceFromFile(ActionEvent e) {
    JFileChooser fc = new JFileChooser();

    if (fc.showOpenDialog((Component) e.getSource()) == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        try {// w  w w .  ja va 2  s .c o m
            this.emissionsequenceModel = new EmissionsequenceModel(
                    new Scanner(file).useDelimiter("\\A").next());
            this.model = null;
            this.displayStatus("File was read successfully!");
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}

From source file:client.ui.UploadFileWindow.java

private void pathButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_pathButtonActionPerformed
    JFileChooser fileWindow = new JFileChooser();
    int confirm = fileWindow.showOpenDialog(null);
    if (confirm == JFileChooser.APPROVE_OPTION) {
        String filePath = fileWindow.getSelectedFile().getPath();
        String filename = fileWindow.getSelectedFile().getName();
        pathField.setText(filePath);/*www  . ja  va2s. co  m*/
        filenameField.setText(filename);
    }
}

From source file:MessageDigestTest.java

/**
 * Loads a file and computes its message digest.
 *///from   ww w . j  a  va 2s  .c o  m
public void loadFile() {
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));

    int r = chooser.showOpenDialog(this);
    if (r == JFileChooser.APPROVE_OPTION) {
        try {
            String name = chooser.getSelectedFile().getAbsolutePath();
            computeDigest(loadBytes(name));
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }
}