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:Main.java

public Main() throws Exception {
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);// w  w w.j ava  2s .c o m
    soundFile = chooser.getSelectedFile();

    System.out.println("Playing " + soundFile.getName());

    Line.Info linfo = new Line.Info(Clip.class);
    Line line = AudioSystem.getLine(linfo);
    clip = (Clip) line;
    clip.addLineListener(this);
    AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
    clip.open(ais);
    clip.start();
}

From source file:CoreJavaSound.java

public CoreJavaSound() throws Exception {
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);//from   w  w  w .  j  a v  a2  s .com
    soundFile = chooser.getSelectedFile();

    System.out.println("Playing " + soundFile.getName());

    Line.Info linfo = new Line.Info(Clip.class);
    Line line = AudioSystem.getLine(linfo);
    clip = (Clip) line;
    clip.addLineListener(this);
    AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
    clip.open(ais);
    clip.start();
}

From source file:ActionListenerTest2.java

public void actionPerformed(ActionEvent e) {
    JFileChooser fileChooser = new JFileChooser();
    int returnVal = fileChooser.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        System.out.print(fileChooser.getSelectedFile().getName());
    }/*  w  w w  .ja  v a2 s . c  o  m*/
}

From source file:Main.java

/**
 * Consistent way to chosing a file to open with JFileChooser.
 * <p>/* w  ww.j  ava2 s  .c o  m*/
 * 
 * @see JFileChooser#setFileSelectionMode(int)
 * @see #getSystemFiles(Component, int)
 * @param owner to show the component relative to.
 * @param mode selection mode for the JFileChooser.
 * @return File based on the user selection can be null.
 */
public static File getSystemFile(Component owner, int mode, FileFilter[] filters) {

    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(mode);
    jfc.setFileHidingEnabled(true);
    jfc.setAcceptAllFileFilterUsed(true);
    if (filters != null) {
        for (int i = 0; i < filters.length; i++) {
            jfc.addChoosableFileFilter(filters[i]);
        }

        if (filters.length >= 1) {
            jfc.setFileFilter(filters[0]);
        }
    }

    int result = jfc.showOpenDialog(owner);

    if (result == JFileChooser.APPROVE_OPTION) {
        return jfc.getSelectedFile();
    }

    return null;
}

From source file:Main.java

/**
 * Consistent way to chosing multiple files to open with JFileChooser.
 * <p>/*from w  ww .  j  a v a2 s .  c  o  m*/
 * 
 * @see JFileChooser#setFileSelectionMode(int)
 * @see #getSystemFiles(Component, int)
 * @param owner to show the component relative to.
 * @param mode selection mode for the JFileChooser.
 * @return File[] based on the user selection can be null.
 */
public static File[] getSystemFiles(Component owner, int mode, FileFilter[] filters) {

    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(mode);
    jfc.setFileHidingEnabled(true);
    jfc.setMultiSelectionEnabled(true);
    jfc.setAcceptAllFileFilterUsed(true);
    if (filters != null) {
        for (int i = 0; i < filters.length; i++) {
            jfc.addChoosableFileFilter(filters[i]);
        }

        if (filters.length >= 1) {
            jfc.setFileFilter(filters[0]);
        }
    }

    int result = jfc.showOpenDialog(owner);
    if (result == JFileChooser.APPROVE_OPTION) {
        return jfc.getSelectedFiles();
    }

    return new File[0];
}

From source file:net.rptools.maptool.util.AssetExtractor.java

public static void extract() throws Exception {
    new Thread() {
        @Override//from  w w  w  .j  av  a  2  s.  c  om
        public void run() {
            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(false);
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            if (chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {
                return;
            }
            File file = chooser.getSelectedFile();
            File newDir = new File(file.getParentFile(),
                    file.getName().substring(0, file.getName().lastIndexOf('.')) + "_images");

            JLabel label = new JLabel("", JLabel.CENTER);
            JFrame frame = new JFrame();
            frame.setTitle("Campaign Image Extractor");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setSize(400, 75);
            frame.add(label);
            SwingUtil.centerOnScreen(frame);
            frame.setVisible(true);
            Reader r = null;
            OutputStream out = null;
            PackedFile pakfile = null;
            try {
                newDir.mkdirs();

                label.setText("Loading campaign ...");
                pakfile = new PackedFile(file);

                Set<String> files = pakfile.getPaths();
                XStream xstream = new XStream();
                int count = 0;
                for (String filename : files) {
                    count++;
                    if (filename.indexOf("assets") < 0) {
                        continue;
                    }
                    r = pakfile.getFileAsReader(filename);
                    Asset asset = (Asset) xstream.fromXML(r);
                    IOUtils.closeQuietly(r);

                    File newFile = new File(newDir, asset.getName() + ".jpg");
                    label.setText("Extracting image " + count + " of " + files.size() + ": " + newFile);
                    if (newFile.exists()) {
                        newFile.delete();
                    }
                    newFile.createNewFile();
                    out = new FileOutputStream(newFile);
                    FileUtil.copyWithClose(new ByteArrayInputStream(asset.getImage()), out);
                }
                label.setText("Done.");
            } catch (Exception ioe) {
                MapTool.showInformation("AssetExtractor failure", ioe);
            } finally {
                if (pakfile != null)
                    pakfile.close();
                IOUtils.closeQuietly(r);
                IOUtils.closeQuietly(out);
            }
        }
    }.start();
}

From source file:com.decypher.threadsclient.JPanelChart.java

private String getFolder() {
    JFileChooser folderPick = new JFileChooser();
    folderPick.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    folderPick.setDialogType(JFileChooser.SAVE_DIALOG);
    folderPick.setMultiSelectionEnabled(false);
    String selected;// w  ww .j a  v a2s .c  o  m
    int returnVal = folderPick.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        selected = folderPick.getSelectedFile().getPath();
    } else {
        selected = "";
    }
    return selected;
}

From source file:org.jfreechart.SVGExporter.java

public static Action createExportAction(final JFreeChart chart, final JPanel bounds) {
    return new AbstractAction("Save as SVG...") {

        public void actionPerformed(ActionEvent e) {
            try {
                JFileChooser fc = new JFileChooser();
                fc.showSaveDialog(null);
                if (fc.getSelectedFile() != null) {
                    exportChartAsSVG(chart, bounds.getBounds(), fc.getSelectedFile());
                }//from   ww  w  .  j  ava 2 s  .c om
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    };
}

From source file:Main.java

public void actionPerformed(ActionEvent e) {
    int retVal;//from   w ww .  j av  a 2 s.c o  m

    JFileChooser fc = new JFileChooser();

    if (e.getActionCommand().equals("Open ...")) {
        fc.addChoosableFileFilter(new TextFilter());
        retVal = fc.showOpenDialog(this);
    } else
        retVal = fc.showSaveDialog(this);

    if (retVal == JFileChooser.APPROVE_OPTION)
        System.out.println(fc.getSelectedFile().getName());
}

From source file:Main.java

protected void saveToFile() {
    JFileChooser fileChooser = new JFileChooser();
    int retval = fileChooser.showSaveDialog(save);
    if (retval == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();
        if (file == null) {
            return;
        }//  ww w. java 2  s  .  c o  m
        if (!file.getName().toLowerCase().endsWith(".txt")) {
            file = new File(file.getParentFile(), file.getName() + ".txt");
        }
        try {
            textArea.write(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
            Desktop.getDesktop().open(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}