Example usage for javax.swing JFileChooser setCurrentDirectory

List of usage examples for javax.swing JFileChooser setCurrentDirectory

Introduction

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

Prototype

@BeanProperty(preferred = true, description = "The directory that the JFileChooser is showing files of.")
public void setCurrentDirectory(File dir) 

Source Link

Document

Sets the current directory.

Usage

From source file:Result3.java

public void createImageJPG() {
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("Documents"));
    int retrival = chooser.showSaveDialog(null);
    if (retrival == JFileChooser.APPROVE_OPTION) {
        try {/*from   w  ww  . j a  v a2  s  .  co m*/
            ImageIO.write(bImage1, "jpg", new File(chooser.getSelectedFile() + ".jpg"));
        } catch (IOException ex) {
            Logger.getLogger(Result1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:com.intuit.tank.proxy.settings.ui.ProxyConfigDialog.java

private void save(int port, boolean followRedirecs, String outputFile,
        Set<ConfigInclusionExclusionRule> inclusions, Set<ConfigInclusionExclusionRule> exclusions,
        Set<ConfigInclusionExclusionRule> bodyInclusions, Set<ConfigInclusionExclusionRule> bodyExclusions,
        String configFileName) {/*w  ww  .j a v  a  2  s.c  om*/
    String fileName = "";
    if (StringUtils.isEmpty(configFileName)) {
        JFileChooser fileChooser = new JFileChooser();
        File file = new File(".");
        fileChooser.setCurrentDirectory(file);
        fileChooser.setAcceptAllFileFilterUsed(false);
        fileChooser.setFileFilter(new XmlFileFilter());
        int showSaveDialog = fileChooser.showSaveDialog(this);
        if (showSaveDialog == JFileChooser.APPROVE_OPTION) {
            String selectedFile = fileChooser.getSelectedFile().getName();
            if (!selectedFile.endsWith(".xml")) {
                selectedFile = selectedFile + ".xml";
            }
            fileName = fileChooser.getSelectedFile().getParent() + "/" + selectedFile;
            configHandler.setConfigFile(fileName);
        } else {
            return;
        }
    } else {
        fileName = configFileName;
    }
    CommonsProxyConfiguration.save(port, followRedirecs, outputFile, inclusions, exclusions, bodyInclusions,
            bodyExclusions, fileName);
    getProxyConfigPanel().update();
    configHandler.setConfigFile(fileName);
}

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 .  j  av a2 s. c o  m*/

    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:examples.gp.monalisa.gui.GeneticDrawingView.java

@Action
public void chooseImage() throws IOException {
    JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(new File("."));
    fc.showOpenDialog(mainPanel);/* w  w  w  .jav a2 s.c  o m*/
    File file = fc.getSelectedFile();
    targetImage = ImageIO.read(file);
    targetImageLabel.setIcon(scaleToImageLabel(targetImage));
    fittestDrawingView.setSize(targetImage.getWidth(), targetImage.getHeight());
}

From source file:gd.gui.GeneticDrawingView.java

@Action
public void chooseImage() throws IOException {
    JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(new File("."));
    fc.showOpenDialog(mainPanel);//from  www  .j  a v  a2s .c  o m

    File file = fc.getSelectedFile();
    targetImage = ImageIO.read(file);
    targetImageLabel.setIcon(scaleToImageLabel(targetImage));
    fittestDrawingView.setSize(targetImage.getWidth(), targetImage.getHeight());
}

From source file:XPathTest.java

/**
     * Open a file and load the document.
     *///from   w w w  .  ja  va 2  s .co  m
    public void openFile() {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("."));

        chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
            public boolean accept(File f) {
                return f.isDirectory() || f.getName().toLowerCase().endsWith(".xml");
            }

            public String getDescription() {
                return "XML files";
            }
        });
        int r = chooser.showOpenDialog(this);
        if (r != JFileChooser.APPROVE_OPTION)
            return;
        File f = chooser.getSelectedFile();
        try {
            byte[] bytes = new byte[(int) f.length()];
            new FileInputStream(f).read(bytes);
            docText.setText(new String(bytes));
            doc = builder.parse(f);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, e);
        } catch (SAXException e) {
            JOptionPane.showMessageDialog(this, e);
        }
    }

From source file:net.sf.keystore_explorer.gui.actions.ExamineFileAction.java

private File chooseFile() {
    JFileChooser chooser = FileChooserFactory.getCertFileChooser();
    chooser.setCurrentDirectory(CurrentDirectory.get());
    chooser.setDialogTitle(res.getString("ExamineFileAction.ExamineFile.Title"));
    chooser.setMultiSelectionEnabled(false);

    int rtnValue = chooser.showDialog(frame, res.getString("ExamineFileAction.ExamineFile.button"));
    if (rtnValue == JFileChooser.APPROVE_OPTION) {
        File openFile = chooser.getSelectedFile();
        CurrentDirectory.updateForFile(openFile);
        return openFile;
    }/*w w w .  j  ava  2 s.c  o  m*/
    return null;
}

From source file:net.sf.keystore_explorer.gui.actions.SignCsrAction.java

private File chooseCsrFile() {
    JFileChooser chooser = FileChooserFactory.getCsrFileChooser();
    chooser.setCurrentDirectory(CurrentDirectory.get());
    chooser.setDialogTitle(res.getString("SignCsrAction.ChooseCsr.Title"));
    chooser.setMultiSelectionEnabled(false);

    int rtnValue = chooser.showDialog(frame, res.getString("SignCsrAction.ChooseCsr.button"));
    if (rtnValue == JFileChooser.APPROVE_OPTION) {
        File importFile = chooser.getSelectedFile();
        CurrentDirectory.updateForFile(importFile);
        return importFile;
    }//from  w w  w. ja v  a2  s .  c o  m
    return null;
}

From source file:modelibra.swing.app.util.FileSelector.java

/**
 * Gets the opened file given a file path.
 * /*from   www  .j a  v a  2s .c o m*/
 * @param path
 *            path
 * @param lang
 *            language
 * @return file
 */
public File getOpenFile(String path, NatLang lang) {
    File selectedFile = null;
    File currentFile = null;
    if (path != null && !path.equalsIgnoreCase("?")) {
        currentFile = new File(path);
    } else
        currentFile = lastOpenedFile;

    JFileChooser chooser = new JFileChooser();
    chooser.setLocale(lang.getLocale());
    if (currentFile != null) {
        chooser.setCurrentDirectory(currentFile);
    }
    int returnVal = chooser.showOpenDialog(chooser);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        selectedFile = chooser.getSelectedFile();
    }
    lastOpenedFile = selectedFile;

    return selectedFile;
}

From source file:modelibra.swing.app.util.FileSelector.java

/**
 * Gets the saved file given a file path.
 * /*from ww  w.  j  av a  2 s  .co m*/
 * @param path
 *            path
 * @param lang
 *            language
 * @return file
 */
public File getSaveFile(String path, NatLang lang) {
    File selectedFile = null;
    File currentFile = null;
    if (path != null && !path.equalsIgnoreCase("?")) {
        currentFile = new File(path);
    } else
        currentFile = lastSavedFile;

    JFileChooser chooser = new JFileChooser();
    chooser.setLocale(lang.getLocale());
    if (currentFile != null) {
        chooser.setCurrentDirectory(currentFile);
    }
    int returnVal = chooser.showSaveDialog(chooser);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        selectedFile = chooser.getSelectedFile(); // a complete path
        if (selectedFile.exists()) {
            if (!replaceFile(null, lang)) {
                selectedFile = null;
            }
        }
    }
    lastSavedFile = selectedFile;

    return selectedFile;
}