Example usage for javax.swing JFileChooser JFileChooser

List of usage examples for javax.swing JFileChooser JFileChooser

Introduction

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

Prototype

public JFileChooser() 

Source Link

Document

Constructs a JFileChooser pointing to the user's default directory.

Usage

From source file:de.fhg.igd.mapviewer.server.file.FileTiler.java

/**
 * Load the command paths from the preferences or ask the user for them
 *///  w  w w. ja  va 2 s. c om
private void loadCommandPaths() {
    String convert = pref.get(PREF_CONVERT, null);
    String identify = pref.get(PREF_IDENTIFY, null);

    JFileChooser commandChooser = new JFileChooser();

    if (convert != null && identify != null) {
        if (JOptionPane.showConfirmDialog(null,
                "<html>Found paths to executables:<br/><b>" + convert + "<br/>" + identify
                        + "</b><br/>Do you want to use this settings?</html>",
                "Paths to executables", JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE) == JOptionPane.NO_OPTION) {
            convert = null;
            identify = null;
        }
    }

    if (convert == null) {
        // ask for convert path
        convert = askForPath(commandChooser, new ContainsFileFilter("convert"),
                "Please select your convert executable");
    }

    if (convert != null && identify == null) {
        // ask for identify path
        identify = askForPath(commandChooser, new ContainsFileFilter("identify"),
                "Please select your identify executable");
    }

    if (convert == null)
        pref.remove(PREF_CONVERT);
    else
        pref.put(PREF_CONVERT, convert);

    if (identify == null)
        pref.remove(PREF_IDENTIFY);
    else
        pref.put(PREF_IDENTIFY, identify);

    convertPath = convert;
    identifyPath = identify;
}

From source file:mergedoc.ui.FileChooserField.java

/**
 * ??????
 * @param mode ?
 */
public void setSelectionMode(SelectionMode mode) {
    chooser = new JFileChooser();
    mode.apply(chooser);
}

From source file:jpad.MainEditor.java

public void openFile() {
    if (_isOSX || ostype == ostype.Linux || ostype == ostype.Other) {
        openFile_OSX_Nix();//from w  w  w .  j a  va  2s  .c o  m
    } else {
        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle("Open a Text File");
        fc.setFileFilter(new FileNameExtensionFilter("Plain Text Files", "txt"));
        int returnval = fc.showOpenDialog(this);
        if (returnval == 0) {
            hasSavedToFile = true;
            hasChanges = false;
            curFile = fc.getSelectedFile().getAbsolutePath();
            try (FileInputStream stream = new FileInputStream(curFile)) {
                String file = IOUtils.toString(stream);
                mainTextArea.setText(file);
                this.setTitle(String.format("JPad - %s", curFile));
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}

From source file:it.unibo.alchemist.boundary.gui.Perspective.java

private void openFile() {
    final JFileChooser fc = new JFileChooser();
    fc.setMultiSelectionEnabled(false);/*from  w ww  .j  av a2  s.  com*/
    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:uk.co.petertribble.jkstat.gui.KstatBaseChartFrame.java

/**
 * Saves the current chart as an image in png format. The user can select
 * the filename, and is asked to confirm the overwrite of an existing file.
 *//*from   ww w.  j  a  v  a2s  .  c  o  m*/
public void saveImage() {
    JFileChooser fc = new JFileChooser();
    if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
        File f = fc.getSelectedFile();
        if (f.exists()) {
            int ok = JOptionPane.showConfirmDialog(this,
                    KstatResources.getString("SAVEAS.OVERWRITE.TEXT") + " " + f.toString(),
                    KstatResources.getString("SAVEAS.CONFIRM.TEXT"), JOptionPane.YES_NO_OPTION);
            if (ok != JOptionPane.YES_OPTION) {
                return;
            }
        }
        BufferedImage bi = kbc.getChart().createBufferedImage(500, 300);
        try {
            ImageIO.write(bi, "png", f);
            /*
             * According to the API docs this should throw an IOException
             * on error, but this doesn't seem to be the case. As a result
             * it's necessary to catch exceptions more generally. Even this
             * doesn't work properly, but at least we manage to convey the
             * message to the user that the write failed, even if the
             * error itself isn't handled.
             */
        } catch (Exception ioe) {
            JOptionPane.showMessageDialog(this, ioe.toString(), KstatResources.getString("SAVEAS.ERROR.TEXT"),
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}

From source file:com.intuit.tank.tools.script.ScriptFilterRunner.java

/**
 * @return//from  w  w w .  j  ava  2 s  . c om
 */
private Component createTopPanel() {
    JPanel topPanel = new JPanel(new GridBagLayout());
    List<ConfiguredLanguage> configuredLanguages = ConfiguredLanguage.getConfiguredLanguages();
    languageSelector = new JComboBox(configuredLanguages.toArray());
    languageSelector.setSelectedIndex(0);
    languageSelector.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            setSyntaxStyle();
        }
    });

    currentFileLabel = new JLabel();

    final JFileChooser jFileChooser = new JFileChooser();
    jFileChooser.setFileFilter(new FileFilter() {

        @Override
        public String getDescription() {
            return "Tank XML Files";
        }

        @Override
        public boolean accept(File f) {
            return f.isDirectory() || f.getName().toLowerCase().endsWith("_ts.xml");
        }
    });

    JButton button = new JButton("Select File...");
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            int showOpenDialog = jFileChooser.showOpenDialog(ScriptFilterRunner.this);
            if (showOpenDialog == JFileChooser.APPROVE_OPTION) {
                loadTSXml(jFileChooser.getSelectedFile());
            }
        }

    });
    xmlViewDialog = new XMlViewDialog(this);
    xmlViewDialog.setSize(new Dimension(800, 500));
    showXmlBT = new JButton("Show XML");
    showXmlBT.setEnabled(false);
    showXmlBT.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            displayXml();
        }

    });

    saveBT = new JButton("Save XML");
    saveBT.setEnabled(false);
    saveBT.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            saveXml();
        }

    });

    JPanel xmlPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 10, 20));
    xmlPanel.add(button);
    xmlPanel.add(showXmlBT);
    xmlPanel.add(saveBT);

    int y = 0;

    topPanel.add(new JLabel("Current Tank XML: "), getConstraints(0, y, GridBagConstraints.NONE));
    topPanel.add(currentFileLabel, getConstraints(1, y++, GridBagConstraints.HORIZONTAL));

    topPanel.add(new JLabel("Select Script Language: "), getConstraints(0, y, GridBagConstraints.NONE));
    topPanel.add(languageSelector, getConstraints(1, y++, GridBagConstraints.HORIZONTAL));

    topPanel.add(new JLabel("Select Tank XML: "), getConstraints(0, y, GridBagConstraints.NONE));
    topPanel.add(xmlPanel, getConstraints(1, y++, GridBagConstraints.HORIZONTAL));
    return topPanel;
}

From source file:eu.novait.imagerenamer.gui.MainWindow.java

protected void saveFiles() {
    if (chooser == null) {
        chooser = new JFileChooser();
    }//  w  w  w .j a  v a 2s  .  c o  m
    for (FileFilter ff : chooser.getChoosableFileFilters()) {
        chooser.removeChoosableFileFilter(ff);
    }
    chooser.setMultiSelectionEnabled(false);
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setAcceptAllFileFilterUsed(false);
    int returnVal = chooser.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        loaderBar.setMaximum(imageTableModel.getRowCount());
        loaderBar.setValue(0);
        this.setCurrentState(STATE_SAVING);
        Thread renameThread = new Thread(new Runnable() {

            @Override
            public void run() {
                File dstDir = chooser.getSelectedFile();
                for (ImageFile imageFile : imageTableModel.getList()) {
                    System.out.println(dstDir.getAbsolutePath());
                    loaderBar.setValue(loaderBar.getValue() + 1);
                    //FileUtils.copyFile(dstDir, dstDir);
                }
                setCurrentState(STATE_WAITING);
            }
        });
    }
}

From source file:view.MainFrame.java

private void menuItemOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemOpenActionPerformed
    // TODO add your handling code here:
    JFileChooser filech = new JFileChooser();
    filech.setFileSelectionMode(JFileChooser.FILES_ONLY);
    filech.setFileFilter(new FileNameExtensionFilter("WAV Files", "wav"));
    int ret = filech.showOpenDialog(this);

    if (ret == JFileChooser.APPROVE_OPTION) {
        try {/*from  w ww.java 2  s  .  c  o  m*/
            wavFile = WavFile.openWavFile(filech.getSelectedFile());
            textArea.append(wavFile.getInfoString());
            samples = new double[wavFile.getNumChannels() * (int) wavFile.getSampleRate()];
            int nFrames = wavFile.readFrames(samples, (int) wavFile.getSampleRate());
            textArea.append(nFrames + " frames lidos.\n");

            double valuesX[] = new double[samples.length];
            for (int i = 0; i < samples.length; i++) {
                valuesX[i] = i;
            }

            showChart(wavFile.getNumChannels() * (int) wavFile.getSampleRate(), samples, valuesX, "Amostra",
                    "Amplitude", "Amplitude das Amostras do ?udio", "Audio");
        } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (WavFileException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:GraphUI.java

private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBtnActionPerformed
    int width = 640;
    int height = 480;
    fc = new JFileChooser();
    fc.addChoosableFileFilter(new FileNameExtensionFilter("PNG (*.png)", "png"));
    fc.setAcceptAllFileFilterUsed(false);
    fc.showSaveDialog(null);/* w w w.  j ava 2 s .com*/

    String path = fc.getSelectedFile().getAbsolutePath() + ".png";
    String filename = fc.getSelectedFile().getName() + ".png";
    File imageFile = new File(path);

    /*
    System.out.println("getCurrentDirectory(): "
        + f.getCurrentDirectory());
    System.out.println("getSelectedFile() : "
        + f.getSelectedFile());
     */
    try {
        ChartUtilities.saveChartAsPNG(imageFile, chart, width, height);
        JOptionPane.showMessageDialog(null, "Graph saved as " + filename);
    } catch (IOException ex) {
        System.err.println(ex);
    }

}

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);/* ww w . j ava 2 s. c  om*/
        filenameField.setText(filename);
    }
}