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:edu.umich.robot.ViewerApplication.java

private Config promptForConfig() {
    JFileChooser fc = new JFileChooser("config");
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    FileFilter filter = new FileNameExtensionFilter("Text Config File", "txt");
    fc.setFileFilter(filter);// w  ww  . j a  va  2  s .  c o m
    fc.setMultiSelectionEnabled(false);
    int ret = fc.showOpenDialog(frame);
    if (ret == JFileChooser.APPROVE_OPTION) {
        try {
            return new ConfigFile(fc.getSelectedFile().getAbsolutePath());
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
    }
    return null;
}

From source file:chibi.gemmaanalysis.cli.deprecated.ProbeMapperGui.java

/**
 * This method initializes InputFileBrowseButton
 * /*from  ww  w. j  a v  a2s. c  o m*/
 * @return javax.swing.JButton
 */
private JButton getInputFileBrowseButton() {
    if (inputFileBrowseButton == null) {
        inputFileBrowseButton = new JButton();
        inputFileBrowseButton.setText("Browse...");
        inputFileBrowseButton.setActionCommand("inputFileBrowse");
        inputFileBrowseButton.setBounds(451, 8, 87, 26);
        inputFileBrowseButton.addActionListener(new ActionListener() {

            @Override
            @SuppressWarnings({ "synthetic-access" })
            public void actionPerformed(ActionEvent e) {

                JFileChooser fc = new JFileChooser();
                fc.showOpenDialog(jContentPane.getParent());
                File selectedFile = fc.getSelectedFile();

                if (selectedFile != null) {
                    getInputFileNameTextField().setText(selectedFile.getAbsolutePath());
                    if (!selectedFile.canRead()) {
                        // error
                    } else {
                        inputFile = selectedFile;
                    }
                }
            }
        });
    }
    return inputFileBrowseButton;
}

From source file:net.panthema.BispanningGame.MyGraphMLReader.java

MyGraphMLReader(javax.swing.JPanel panel) throws FileNotFoundException, GraphIOException {

    posMap = new TreeMap<Integer, Point2D>();

    // Query user for filename
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle("Specify GraphML file to read");
    chooser.setCurrentDirectory(new File("."));
    FileNameExtensionFilter filter = new FileNameExtensionFilter("GraphML File", "graphml");
    chooser.setFileFilter(filter);/*from ww  w  .  ja va  2  s.c om*/

    if (chooser.showOpenDialog(panel) != JFileChooser.APPROVE_OPTION)
        return;

    File infile = chooser.getSelectedFile();

    BufferedReader fileReader = new BufferedReader(new FileReader(infile));

    newGraph = new MyGraph();

    // create the graph transformer
    Transformer<GraphMetadata, MyGraph> graphTransformer = new Transformer<GraphMetadata, MyGraph>() {
        public MyGraph transform(GraphMetadata metadata) {
            assert (metadata.getEdgeDefault().equals(EdgeDefault.UNDIRECTED));
            return newGraph;
        }
    };

    // create the vertex transformer
    Transformer<NodeMetadata, Integer> vertexTransformer = new Transformer<NodeMetadata, Integer>() {
        public Integer transform(NodeMetadata metadata) {
            // create a new vertex
            Integer v = newGraph.getVertexCount();

            // save layout information
            if (metadata.getProperty("x") != null && metadata.getProperty("y") != null) {
                double x = Double.parseDouble(metadata.getProperty("x"));
                double y = Double.parseDouble(metadata.getProperty("y"));
                posMap.put(v, new Point2D.Double(x, y));
            }

            newGraph.addVertex(v);
            return v;
        }
    };

    // create the edge transformer
    Transformer<EdgeMetadata, MyEdge> edgeTransformer = new Transformer<EdgeMetadata, MyEdge>() {
        public MyEdge transform(EdgeMetadata metadata) {
            MyEdge e = new MyEdge(newGraph.getEdgeCount());
            if (metadata.getProperty("color") != null)
                e.color = Integer.parseInt(metadata.getProperty("color"));
            return e;
        }
    };

    // create the useless hyperedge transformer
    Transformer<HyperEdgeMetadata, MyEdge> hyperEdgeTransformer = new Transformer<HyperEdgeMetadata, MyEdge>() {
        public MyEdge transform(HyperEdgeMetadata metadata) {
            return null;
        }
    };

    // create the graphMLReader2
    GraphMLReader2<MyGraph, Integer, MyEdge> graphReader = new GraphMLReader2<MyGraph, Integer, MyEdge>(
            fileReader, graphTransformer, vertexTransformer, edgeTransformer, hyperEdgeTransformer);

    // Get the new graph object from the GraphML file
    graphReader.readGraph();
}

From source file:edu.harvard.i2b2.adminTool.dataModel.PatientIDConversionFactory.java

private String getKey() {
    String path = null;/*from   w w  w . j ava  2  s  . c om*/
    String key = UserInfoBean.getInstance().getKey();
    if (key == null) {
        if ((path = getNoteKeyDrive()) == null) {
            Object[] possibleValues = { "Type in the key", "Browse to find the file containing the key" };
            String selectedValue = (String) JOptionPane.showInputDialog(null,
                    "You have selected an item associated with a report\n"
                            + "which contains protected health information.\n"
                            + "You need a decryption key to perform this operation.\n"
                            + "How would you like to enter the key?\n"
                            + "(If the key is on a floppy disk, insert the disk then\n select "
                            + "\"Browse to find the file containing the key\")",
                    "Notes Viewer", JOptionPane.QUESTION_MESSAGE, null, possibleValues, possibleValues[0]);
            if (selectedValue == null) {
                return "Not a valid key";
            }
            if (selectedValue.equalsIgnoreCase("Type in the key")) {
                key = JOptionPane.showInputDialog(this, "Please input the decryption key");
                if (key == null) {
                    return "Not a valid key";
                }
            } else {
                JFileChooser chooser = new JFileChooser();
                int returnVal = chooser.showOpenDialog(null);
                if (returnVal == JFileChooser.CANCEL_OPTION) {
                    return "Not a valid key";
                }

                File f = null;
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    f = chooser.getSelectedFile();
                    System.out.println("Open this file: " + f.getAbsolutePath());

                    BufferedReader in = null;
                    try {
                        in = new BufferedReader(new FileReader(f.getAbsolutePath()));
                        String line = null;
                        while ((line = in.readLine()) != null) {
                            if (line.length() > 0) {
                                key = line.substring(line.indexOf("\"") + 1, line.lastIndexOf("\""));
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (in != null) {
                            try {
                                in.close();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        } else {
            System.out.println("Found key file: " + path);
            BufferedReader in = null;
            try {
                in = new BufferedReader(new FileReader(path));
                String line = null;
                while ((line = in.readLine()) != null) {
                    if (line.length() > 0) {
                        key = line.substring(line.indexOf("\"") + 1, line.lastIndexOf("\""));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    if (key == null) {
        return null;
    } else {
        UserInfoBean.getInstance().setKey(key);
    }
    return key;
}

From source file:pt.lsts.neptus.console.plugins.AirOSPeers.java

public AirOSPeers(ConsoleLayout console) {
    super(console);
    setLayout(new BorderLayout());
    chart = ChartFactory.createTimeSeriesChart(null, "Time of day", "Link Quality", tsc, true, true, true);
    chart.getPlot().setBackgroundPaint(Color.black);
    cpanel = new ChartPanel(chart);
    add(cpanel, BorderLayout.CENTER);
    cpanel.getPopupMenu().add(I18n.text("Load Addresses")).addActionListener(new ActionListener() {
        @Override//w  ww  .j a  v  a 2 s.  c om
        public void actionPerformed(ActionEvent e) {
            try {
                JFileChooser chooser = new JFileChooser();
                chooser.setFileFilter(GuiUtils.getCustomFileFilter("CSV Files", "csv"));
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int op = chooser.showOpenDialog(AirOSPeers.this);
                if (op == JFileChooser.APPROVE_OPTION)
                    WiFiMacAddresses.parseAddresses(new FileReader(chooser.getSelectedFile()));
            } catch (Exception ex) {
                GuiUtils.errorMessage(getConsole(), ex);
                ex.printStackTrace();
            }
        }
    });
    cpanel.getPopupMenu().addSeparator();
    cpanel.getPopupMenu().add(I18n.text("Clear")).addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            tsc.removeAllSeries();
        }
    });
}

From source file:TextFileHandler.java

public File[] openFiles(String title) {
    File[] result = null;/*from w w  w  .j  a va2  s  .  co  m*/
    JFileChooser chooser = new JFileChooser(new File("."));
    if (title != null)
        chooser.setDialogTitle(title);
    chooser.setMultiSelectionEnabled(true);
    int retVal = chooser.showOpenDialog(null);
    if (retVal == JFileChooser.APPROVE_OPTION) {
        result = chooser.getSelectedFiles();
    }
    return result;
}

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();
            }//from ww w.jav a2  s  . co 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:it.unibo.alchemist.boundary.gui.Perspective.java

private void openFile() {
    final JFileChooser fc = new JFileChooser();
    fc.setMultiSelectionEnabled(false);//from ww w .j a v a  2s  . co  m
    fc.setFileFilter(FILE_FILTER);
    fc.setCurrentDirectory(currentDirectory);
    final int returnVal = fc.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        fileToLoad = fc.getSelectedFile();
        currentDirectory = fc.getSelectedFile().getParentFile();
        if (fileToLoad.exists()) {
            status.setText(getString("ready_to_process") + " " + fileToLoad.getAbsolutePath());
            status.setOK();
            if (sim != null) {
                sim.addCommand(new Engine.StateCommand<T>().stop().build());
            }
            bar.setFileOK(true);
        } else {
            status.setText(FILE_NOT_VALID + " " + fileToLoad.getAbsolutePath());
            status.setNo();
            bar.setFileOK(false);
        }
    }
}

From source file:IHM.FenetreAjoutPhoto.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

    JFileChooser chooser = new JFileChooser(new File("."));

    chooser.setDialogTitle("ouvrir une image");

    int reponse = chooser.showOpenDialog(this);

    if (reponse == JFileChooser.APPROVE_OPTION) {

        InputStream input = null;

        f = chooser.getSelectedFile();/* w  w  w .  j a  va2  s  .  c  o  m*/

        nomPhoto = f.getName();
        nomF = f.getAbsolutePath();

        txtNomPhoto.setText(nomPhoto);

    }

}

From source file:com.naval.gui.Gui.java

private void creerPartie() {
    final JFileChooser fc = new JFileChooser(Config.getRepTravail());

    fc.addChoosableFileFilter(new GameFileFilter("gm", "Description de partie"));
    fc.setAcceptAllFileFilterUsed(false);

    int returnVal = fc.showOpenDialog(frame);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();

        // TODO : load only if game is !=

        try {//from   w  w  w. java  2s .c  o m
            FileReader fr = new FileReader(file);
            partie = Partie.creer(fr);
            partie.save();
            hintBar.setText("Partie " + partie.nom + " cre avec succes");
        } catch (FileNotFoundException e) {
            hintBar.setText(e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            hintBar.setText(e.getMessage());
            e.printStackTrace();
        }
    }
}