Example usage for javax.swing JFileChooser getSelectedFile

List of usage examples for javax.swing JFileChooser getSelectedFile

Introduction

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

Prototype

public File getSelectedFile() 

Source Link

Document

Returns the selected file.

Usage

From source file:cmsc105_mp2.Panels.java

public void createGraph(Data d, float window, Double threshold) {
    XYSeries data = new XYSeries("data");
    double max = Double.MIN_VALUE;
    for (double num : d.plots) {
        if (max < num)
            max = num;/*from  w  ww  .  ja va2 s  .co  m*/
    }
    max += 1;

    ArrayList<XYSeries> xy = new ArrayList();
    ArrayList<Integer> points = new ArrayList();

    for (int j = (int) (window / 2); j < d.plots.length - (window / 2); j++) {
        data.add(j, d.plots[j]);
        //System.out.println(points.size());
        if (d.plots[j] > threshold) {
            points.add(j);
        } else {
            if (points.size() >= window) {
                //System.out.println("MIN!");
                XYSeries series = new XYSeries("trend");
                for (int n : points) {
                    series.add(n, max);
                }
                xy.add(series);
            }
            points = new ArrayList();
        }
    }
    if (points.size() >= window) {
        XYSeries series = new XYSeries("trend");
        for (int n : points) {
            series.add(n, max);
        }
        xy.add(series);
    }
    XYSeriesCollection my_data_series = new XYSeriesCollection();
    my_data_series.addSeries(data);
    for (XYSeries x : xy) {
        my_data_series.addSeries(x);
    }

    XYSeries thresh = new XYSeries("threshold");
    for (int j = 0; j < d.plots.length; j++) {
        thresh.add(j, threshold);
    }
    my_data_series.addSeries(thresh);

    //System.out.println(d.name);
    JFreeChart XYLineChart = ChartFactory.createXYLineChart(d.name, "Position", "Average", my_data_series,
            PlotOrientation.VERTICAL, true, true, false);
    bImage1 = (BufferedImage) XYLineChart.createBufferedImage(690, 337);
    imageIcon = new ImageIcon(bImage1);
    jLabel1.setIcon(imageIcon);
    jLabel1.revalidate();
    jLabel1.repaint();
    jButton1.setText("Save as PNG");
    jButton1.repaint();
    jButton1.revalidate();
    jButton1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            chooser.setCurrentDirectory(new File("Documents"));
            int retrival = chooser.showSaveDialog(null);
            if (retrival == JFileChooser.APPROVE_OPTION) {
                try {
                    ImageIO.write(bImage1, "png", new File(chooser.getSelectedFile() + ".png"));
                } catch (IOException ex) {
                    System.out.println("Unable to Print!");
                }
            }
        }
    });
}

From source file:net.atomique.ksar.ui.GraphView.java

private String askSaveFilename(String title, File chdirto) {
    String filename = null;//w w w  . j  a  v a 2s.  c om
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle(title);
    if (chdirto != null) {
        chooser.setCurrentDirectory(chdirto);
    }
    int returnVal = chooser.showSaveDialog(GlobalOptions.getUI());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        filename = chooser.getSelectedFile().getAbsolutePath();
    }

    if (filename == null) {
        return null;
    }

    if (new File(filename).exists()) {
        String[] choix = { "Yes", "No" };
        int resultat = JOptionPane.showOptionDialog(null, "Overwrite " + filename + " ?", "File Exist",
                JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, choix, choix[1]);
        if (resultat != 0) {
            return null;
        }
    }
    return filename;
}

From source file:cc.creativecomputing.io.CCIOUtil.java

/**
 * Opens a platform-specific file chooser dialog to select a file for input. This function returns the full path to
 * the selected file as a <b>String</b>, or <b>null</b> if no selection. Files are filtered according to the given
 * file extensions.//  ww w .ja v a  2  s.c om
 * 
 * @webref input:files
 * @param theExtensions file extensions for filtering
 * @return full path to the selected file, or null if canceled.
 * 
 * @see #selectOutput(String)
 * @see #selectFolder(String)
 */
static public String selectFilteredInput(String... theExtensions) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileFilter(new CCFileExtensionFilter(theExtensions));
    fileChooser.setDialogTitle("Select a file...");

    int returned = fileChooser.showOpenDialog(null);
    if (returned == JFileChooser.CANCEL_OPTION) {
        selectedFile = null;
    } else {
        selectedFile = fileChooser.getSelectedFile();
    }
    return (selectedFile == null) ? null : selectedFile.getAbsolutePath();
}

From source file:it.unibas.spicygui.controllo.file.ActionSaveMappingTask.java

private void richiediUtilizzoFileChooser(MappingTask mappingTask, File file)
        throws HeadlessException, MissingResourceException {
    JFileChooser chooser = vista.getFileChooserSalvaXmlXsd();
    continua = true;//from ww  w  . j a  v a 2 s  . c o m
    while (continua) {
        int returnVal = chooser.showSaveDialog(WindowManager.getDefault().getMainWindow());
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                file = chooser.getSelectedFile();
                if (!file.exists()) {
                    daoMappingTask.saveMappingTask(mappingTask, file.getAbsolutePath());
                    mappingTask.setModified(false);
                    mappingTask.setToBeSaved(false);
                    continua = false;
                    esito = true;
                    // modello.putBean(Costanti.ACTUAL_SAVE_FILE, file);
                    Scenario scenario = (Scenario) modello.getBean(Costanti.CURRENT_SCENARIO);
                    scenario.setSaveFile(file);
                    MappingTaskTopComponent mappingTaskTopComponent = scenario.getMappingTaskTopComponent();
                    mappingTaskTopComponent.initNameTab();
                    enableActions();
                } else {
                    chiediConferma(file, mappingTask);
                }
            } catch (DAOException ex) {
                DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
                        NbBundle.getMessage(Costanti.class, Costanti.SAVE_ERROR) + " : " + ex.getMessage(),
                        DialogDescriptor.ERROR_MESSAGE));
                logger.error(ex);
                continua = false;
                esito = true;
            }
        } else {
            continua = false;
            esito = false;
        }
    }
}

From source file:it.ferogrammer.ui.MainAppFrame.java

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
    if (jPanel2.getComponentCount() == 1 && (jPanel2.getComponent(0) instanceof ElectropherogramChartPanel)) {
        ElectropherogramChartPanel chartPanel = (ElectropherogramChartPanel) jPanel2.getComponent(0);
        final JFileChooser fc = new JFileChooser();
        //            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = fc.showSaveDialog(chartPanel);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            if (!file.getName().endsWith(".png")) {
                int extindex = file.getPath().indexOf('.') == -1 ? file.getPath().length()
                        : file.getPath().indexOf('.');
                file = new File(file.getPath().substring(0, extindex) + ".png");
            }/*from  ww w  .j  ava2  s . com*/
            try {
                ChartUtilities.saveChartAsPNG(file, chartPanel.getChart(), chartPanel.getWidth(),
                        chartPanel.getHeight());
            } catch (IOException ex) {
                JOptionPane op = new JOptionPane();
                op.showMessageDialog(jPanel2, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    } else {
        JOptionPane op = new JOptionPane();
        op.showMessageDialog(jPanel2, "Cannot export image", "Error", JOptionPane.ERROR_MESSAGE);
    }
}

From source file:ImageIOTest.java

/**
 * Save the current image in a file/*from   w w  w. j a va2s.c  o m*/
 * @param formatName the file format
 */
public void saveFile(final String formatName) {
    if (images == null)
        return;
    Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(formatName);
    ImageWriter writer = iter.next();
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));
    String[] extensions = writer.getOriginatingProvider().getFileSuffixes();
    chooser.setFileFilter(new FileNameExtensionFilter("Image files", extensions));

    int r = chooser.showSaveDialog(this);
    if (r != JFileChooser.APPROVE_OPTION)
        return;
    File f = chooser.getSelectedFile();
    try {
        ImageOutputStream imageOut = ImageIO.createImageOutputStream(f);
        writer.setOutput(imageOut);

        writer.write(new IIOImage(images[0], null, null));
        for (int i = 1; i < images.length; i++) {
            IIOImage iioImage = new IIOImage(images[i], null, null);
            if (writer.canInsertImage(i))
                writer.writeInsert(i, iioImage, null);
        }
    } catch (IOException e) {
        JOptionPane.showMessageDialog(this, e);
    }
}

From source file:TextFileHandler.java

public File openFile(String title) {
    File result = null;/*ww  w  . j av  a2s  .  c om*/
    JFileChooser chooser = new JFileChooser(new File("."));
    if (title != null)
        chooser.setDialogTitle(title);
    int retVal = chooser.showOpenDialog(null);
    if (retVal == JFileChooser.APPROVE_OPTION) {
        result = chooser.getSelectedFile();
    }
    return result;
}

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

/**
 * @return javax.swing.JButton//from  w w w .ja  v a2 s .  c  o  m
 */
private JButton getOutputFileBrowseButton() {
    if (outputFileBrowseButton == null) {
        outputFileBrowseButton = new JButton();
        outputFileBrowseButton.setText("Browse...");
        outputFileBrowseButton.setBounds(454, 4, 87, 26);

        outputFileBrowseButton.addActionListener(new ActionListener() {
            @Override
            @SuppressWarnings({ "synthetic-access" })
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser();
                fc.showSaveDialog(jContentPane.getParent());
                File selectedFile = fc.getSelectedFile();
                if (selectedFile != null) {
                    getOutputFileNameTextField().setText(selectedFile.getAbsolutePath());
                    if (selectedFile.canRead()) {
                        outputFile = selectedFile;
                    } else {
                        // error
                    }
                }
            }
        });
    }
    return outputFileBrowseButton;
}

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

/**
 * This method initializes InputFileBrowseButton
 * /*from  www.ja v a  2  s  .  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:task5.deneme.java

private void btn_chooseImageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_chooseImageActionPerformed
    //         TODO add your handling code here:
    String userDir = System.getProperty("user.home");

    JFileChooser fileChooser = new JFileChooser(userDir + "/Desktop");

    int result = fileChooser.showOpenDialog(this);
    if (result == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        try {//from ww w .jav a  2 s .co  m
            img = ImageIO.read(selectedFile);
            getRGBs();

        } catch (IOException ex) {
            Logger.getLogger(deneme.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    r = red.toArray();
    g = green.toArray();
    b = blue.toArray();
    //        
    //        if(red.indexOf(0)==-1 || red.indexOf(255)==-1){
    //            red.sort(null);
    //            System.out.println("Before Streching... Min and Max Value..." );
    //            System.out.println(red.get(0)+"   "+red.get(red.size()-1));
    //            contrastR= red.get(red.size()-1)- red.get(0);
    //            int fmin=red.get(0);
    //            int fmax=red.get(red.size()-1);
    //            for (int i = 0; i < red.size(); i++) {
    //                int temp2=0;
    //                if(((int)r[i])<=fmin)
    //                    temp2=0;
    //                else if(fmin<=((int)r[i]) && ((int)r[i])<= fmax){
    //                    double temp=((((int)r[i])- fmin)/(contrastR));
    //                    temp2=(int) Math.round((temp*255));
    //                }
    //                else if(((int)r[i])>=fmax){
    //                    temp2=255;
    //                }
    //                r[i]=temp2;
    //            }
    //            ArrayList<Integer> tempp = new ArrayList<>();
    //            for (Object r1 : r) {
    //                tempp.add((int) r1);
    //            }
    //            tempp.sort(null);
    //            System.out.println("After Stretching... Min and Max Value...");
    //            System.out.println(tempp.get(0) + "   " + tempp.get(tempp.size() - 1));
    //        }
    //        
    //        
    //        
    //        
    //        
    //        if(green.indexOf(0)==-1 || green.indexOf(255)==-1){
    //            green.sort(null);
    //            contrastG= green.get(green.size()-1)- green.get(0);
    //            int fmin=green.get(0);
    //            int fmax=green.get(green.size()-1);
    //            for (int i = 0; i < green.size(); i++) {
    //                int temp2=0;
    //                if(((int)g[i])<=fmin)
    //                    temp2=0;
    //                else if(fmin<=((int)g[i]) && ((int)g[i])<= fmax){
    //                    double temp=((((int)g[i])- fmin)/(contrastG));
    //                    temp2=(int) Math.round((temp*255));
    //                }
    //                else if(((int)g[i])>=fmax){
    //                    temp2=255;
    //                }
    //                g[i]=temp2;
    //            }
    //        }    
    //        if(blue.indexOf(0)==-1 || blue.indexOf(255)==-1){
    //            blue.sort(null);
    //            contrastB= blue.get(blue.size()-1)- blue.get(0);
    //            int fmin=blue.get(0);
    //            int fmax=blue.get(blue.size()-1);
    //            for (int i = 0; i < blue.size(); i++) {
    //                int temp2=0;
    //                if(((int)b[i])<=fmin)
    //                    temp2=0;
    //                else if(fmin<=((int)b[i]) && ((int)b[i])<= fmax){
    //                    double temp=((((int)b[i])- fmin)/(contrastB));
    //                    temp2=(int) Math.round((temp*255));
    //                }
    //                else if(((int)b[i])>=fmax){
    //                    temp2=255;
    //                }
    //                b[i]=temp2;
    //            }
    //        }

    display();

}