Example usage for javax.swing JFileChooser APPROVE_OPTION

List of usage examples for javax.swing JFileChooser APPROVE_OPTION

Introduction

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

Prototype

int APPROVE_OPTION

To view the source code for javax.swing JFileChooser APPROVE_OPTION.

Click Source Link

Document

Return value if approve (yes, ok) is chosen.

Usage

From source file:io.github.jeremgamer.editor.panels.MusicFrame.java

public MusicFrame(JFrame frame, final GeneralSave gs) {

    ArrayList<BufferedImage> icons = new ArrayList<BufferedImage>();
    try {//from   ww w.j  ava2s  . co  m
        icons.add(ImageIO.read(ImageGetter.class.getResource("icon16.png")));
        icons.add(ImageIO.read(ImageGetter.class.getResource("icon32.png")));
        icons.add(ImageIO.read(ImageGetter.class.getResource("icon64.png")));
        icons.add(ImageIO.read(ImageGetter.class.getResource("icon128.png")));
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    this.setIconImages((List<? extends Image>) icons);

    this.setTitle("Musique");
    this.setSize(new Dimension(300, 225));

    this.addWindowListener(new WindowListener() {
        @Override
        public void windowActivated(WindowEvent event) {
        }

        @Override
        public void windowClosed(WindowEvent event) {
        }

        @Override
        public void windowClosing(WindowEvent event) {
            try {
                gs.save(new File("projects/" + Editor.getProjectName() + "/general.rbd"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (clip != null) {
                clip.stop();
                clip.close();
                try {
                    audioStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void windowDeactivated(WindowEvent event) {
        }

        @Override
        public void windowDeiconified(WindowEvent event) {
        }

        @Override
        public void windowIconified(WindowEvent event) {
        }

        @Override
        public void windowOpened(WindowEvent event) {
        }
    });

    this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

    content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));

    this.setModal(true);
    this.setLocationRelativeTo(frame);

    JPanel properties = new JPanel();
    properties.setBorder(BorderFactory.createTitledBorder("Lecture"));
    ButtonGroup bg = new ButtonGroup();
    bg.add(one);
    bg.add(loop);
    one.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent event) {
            JRadioButton rb = (JRadioButton) event.getSource();
            if (rb.isSelected()) {
                gs.set("music.reading", 0);
                try {
                    gs.save(new File("projects/" + Editor.getProjectName() + "/general.rbd"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (clip != null) {
                    if (clip.isRunning())
                        clip.loop(0);
                }
            }
        }
    });
    loop.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent event) {
            JRadioButton rb = (JRadioButton) event.getSource();
            if (rb.isSelected()) {
                gs.set("music.reading", 1);
                try {
                    gs.save(new File("projects/" + Editor.getProjectName() + "/general.rbd"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (clip != null) {
                    if (clip.isRunning())
                        clip.loop(Clip.LOOP_CONTINUOUSLY);
                }
            }
        }
    });
    properties.add(one);
    properties.add(loop);
    if (gs.getInt("music.reading") == 0) {
        one.setSelected(true);
    } else {
        loop.setSelected(true);
    }

    volume.setMaximum(100);
    volume.setMinimum(0);
    volume.setValue(30);
    volume.setPaintTicks(true);
    volume.setPaintLabels(true);
    volume.setMinorTickSpacing(10);
    volume.setMajorTickSpacing(20);
    volume.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent event) {
            JSlider slider = (JSlider) event.getSource();
            double value = slider.getValue();
            gain = value / 100;
            dB = (float) (Math.log(gain) / Math.log(10.0) * 20.0);
            if (clip != null)
                gainControl.setValue(dB);
            gs.set("music.volume", (int) value);
        }
    });
    volume.setValue(gs.getInt("music.volume"));
    properties.add(volume);
    properties.setPreferredSize(new Dimension(300, 125));

    content.add(properties);

    JPanel browsePanel = new JPanel();
    browsePanel.setBorder(BorderFactory.createTitledBorder(""));
    JButton browse = new JButton("Parcourir...");
    if (new File("projects/" + Editor.getProjectName() + "/music.wav").exists()) {
        preview.setEnabled(false);
        browse.setText("");
        try {
            browse.setIcon(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    browse.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            JButton button = (JButton) event.getSource();
            if (new File("projects/" + Editor.getProjectName() + "/music.wav").exists()) {
                if (clip != null) {
                    clip.stop();
                    clip.close();
                    try {
                        audioStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                name.setText("");
                preview.setEnabled(false);
                button.setText("Parcourir...");
                button.setIcon(null);
                new File("projects/" + Editor.getProjectName() + "/music.wav").delete();
                gs.set("music.name", "");
            } else {
                String path = null;
                JFileChooser chooser = new JFileChooser(Editor.lastPath);
                FileNameExtensionFilter filter = new FileNameExtensionFilter("Audio (WAV)", "wav");
                chooser.setFileFilter(filter);
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int option = chooser.showOpenDialog(null);
                if (option == JFileChooser.APPROVE_OPTION) {
                    path = chooser.getSelectedFile().getAbsolutePath();
                    Editor.lastPath = chooser.getSelectedFile().getParent();
                    copyMusic(new File(path));
                    button.setText("");
                    try {
                        button.setIcon(
                                new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png"))));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    gs.set("music.name", new File(path).getName());
                    try {
                        gs.save(new File("projects/" + Editor.getProjectName() + "/general.rbd"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    name.setText(new File(path).getName());
                    preview.setEnabled(true);
                }
            }
        }

    });
    if (new File("projects/" + Editor.getProjectName() + "/music.wav").exists()) {
        preview.setEnabled(true);
    } else {
        preview.setEnabled(false);
    }
    preview.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            JToggleButton tb = (JToggleButton) event.getSource();
            if (tb.isSelected()) {
                try {
                    audioStream = AudioSystem.getAudioInputStream(
                            new File("projects/" + Editor.getProjectName() + "/music.wav"));
                    format = audioStream.getFormat();
                    info = new DataLine.Info(Clip.class, format);
                    clip = (Clip) AudioSystem.getLine(info);
                    clip.open(audioStream);
                    clip.start();
                    gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
                    gainControl.setValue(dB);
                    if (loop.isSelected()) {
                        clip.loop(Clip.LOOP_CONTINUOUSLY);
                    } else {
                        clip.loop(0);
                    }
                    clip.addLineListener(new LineListener() {
                        @Override
                        public void update(LineEvent event) {
                            Clip clip = (Clip) event.getSource();
                            if (!clip.isRunning()) {
                                preview.setSelected(false);
                                clip.stop();
                                clip.close();
                                try {
                                    audioStream.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                    });
                } catch (Exception exc) {
                    exc.printStackTrace();
                }
            } else {
                clip.stop();
                clip.close();
                try {
                    audioStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    });
    JPanel buttons = new JPanel();
    buttons.setLayout(new BorderLayout());
    buttons.add(browse, BorderLayout.WEST);
    buttons.add(preview, BorderLayout.EAST);
    browsePanel.setLayout(new BorderLayout());
    browsePanel.add(buttons, BorderLayout.NORTH);
    browsePanel.add(name, BorderLayout.SOUTH);

    name.setPreferredSize(new Dimension(280, 25));
    name.setText(gs.getString("music.name"));
    content.add(browsePanel);

    this.setContentPane(content);
    this.setVisible(true);
}

From source file:org.streamspinner.harmonica.application.CQGraphTerminal.java

private void show_open_hamql_dialog() {
    FileFilter filter = createFilter();

    JFileChooser jf = new JFileChooser("conf/query/");
    jf.setFileFilter(filter);//from  w w w  .j  av a2  s . c o  m

    jf.setDialogTitle("HamQL (SpinQL) ?J");

    int returnVal = jf.showOpenDialog(this);
    if (returnVal != JFileChooser.APPROVE_OPTION)
        return;

    File f = jf.getSelectedFile();

    try {
        FileReader reader = new FileReader(f);
        BufferedReader br = new BufferedReader(reader);

        StringBuilder buf = new StringBuilder();
        String tmp = null;
        while ((tmp = br.readLine()) != null) {
            buf.append(tmp + "\n");
        }
        br.close();
        reader.close();

        getJTextArea().setText(buf.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Compare.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    // TODO add your handling code here:

    String userDir = System.getProperty("user.home");
    JFileChooser folder = new JFileChooser(userDir + "/Desktop");
    folder.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    folder.setFileSelectionMode(JFileChooser.FILES_ONLY);
    FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter("Excel Files  (*.xls)", "xls");
    folder.setFileFilter(xmlfilter);/*from   w  ww .jav a  2  s. c om*/
    int returnvalue = folder.showSaveDialog(this);

    File myfolder = null;
    if (returnvalue == JFileChooser.APPROVE_OPTION) {
        myfolder = folder.getSelectedFile();
        //            System.out.println(myfolder);         
    }

    if (myfolder != null) {
        JOptionPane.showMessageDialog(null, "The current choosen file directory is : " + myfolder);

        listofFiles1(myfolder);
        sortByName1();
    }

    DefaultTableModel model = (DefaultTableModel) FileDetails1.getModel();

    int count = 1;

    System.out.println(filenames2.size());
    for (Files filename : filenames2) {
        String size = Long.toString(filename.Filesize) + "Bytes";
        model.addRow(new Object[] { count++, filename.Filename, size, filename.FileLocation });
    }

}

From source file:de.evaluationtool.gui.EvaluationFrameActionListener.java

private void loadPositiveNegativeNT() throws IOException {
    JFileChooser chooser = new JFileChooser("Load multiple nt files. Please choose a directory");
    chooser.setCurrentDirectory(frame.defaultDirectory);
    if (geoFile != null) {
        chooser.setCurrentDirectory(geoFile);
    }/*from  w  w  w .j av a  2  s .c  o m*/
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setAcceptAllFileFilterUsed(false);

    int returnVal = chooser.showSaveDialog(frame);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        System.out.print("Loading...");
        frame.loadPositiveNegativeNT(chooser.getSelectedFile());
        System.out.println("loading finished.");
    }
}

From source file:com.simplexrepaginator.RepaginateFrame.java

protected JButton creatOutputButton() {
    JButton b = new JButton("Click or drag to set output file", PDF_1234);

    b.setHorizontalTextPosition(SwingConstants.LEFT);
    b.setIconTextGap(25);// w w w .ja  v a  2  s  . c  o  m

    b.setTransferHandler(new OutputButtonTransferHandler());

    b.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(false);
            chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            if (chooser.showOpenDialog(RepaginateFrame.this) != JFileChooser.APPROVE_OPTION)
                return;
            repaginator.setOutputFiles(Arrays.asList(chooser.getSelectedFiles()));
            output.setText("<html><center>" + StringUtils.join(repaginator.getOutputFiles(), "<br>"));
        }
    });

    return b;
}

From source file:net.sf.keystore_explorer.gui.dialogs.DViewCertCsrPem.java

private void exportPressed() {
    File chosenFile = null;/*from  ww w  .  j  av  a2  s.co  m*/
    FileWriter fw = null;

    String title;
    if (cert != null) {
        title = res.getString("DViewCertCsrPem.ExportPemCertificate.Title");
    } else {
        title = res.getString("DViewCertCsrPem.ExportPemCsr.Title");
    }
    try {
        String certPem = jtaPem.getText();

        JFileChooser chooser = FileChooserFactory.getX509FileChooser();
        chooser.setCurrentDirectory(CurrentDirectory.get());
        chooser.setDialogTitle(title);
        chooser.setMultiSelectionEnabled(false);

        int rtnValue = JavaFXFileChooser.isFxAvailable() ? chooser.showSaveDialog(this)
                : chooser.showDialog(this, res.getString("DViewCertCsrPem.ChooseExportFile.button"));

        if (rtnValue != JFileChooser.APPROVE_OPTION) {
            return;
        }

        chosenFile = chooser.getSelectedFile();
        CurrentDirectory.updateForFile(chosenFile);

        if (chosenFile.isFile()) {
            String message = MessageFormat.format(res.getString("DViewCertCsrPem.OverWriteFile.message"),
                    chosenFile);

            int selected = JOptionPane.showConfirmDialog(this, message, title, JOptionPane.YES_NO_OPTION);
            if (selected != JOptionPane.YES_OPTION) {
                return;
            }
        }

        fw = new FileWriter(chosenFile);
        fw.write(certPem);
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(this,
                MessageFormat.format(res.getString("DViewCertCsrPem.NoWriteFile.message"), chosenFile), title,
                JOptionPane.WARNING_MESSAGE);
        return;
    } catch (Exception ex) {
        DError.displayError(this, ex);
        return;
    } finally {
        IOUtils.closeQuietly(fw);
    }

    JOptionPane.showMessageDialog(this, res.getString("DViewCertCsrPem.ExportPemCertificateSuccessful.message"),
            title, JOptionPane.INFORMATION_MESSAGE);
}

From source file:com.holycityaudio.SpinCAD.SpinCADFile.java

public SpinCADPatch fileOpenPatch() {

    loadRecentPatchFileList();//  w  w  w. j  a va 2 s .  co  m
    final String newline = "\n";
    SpinCADPatch p = new SpinCADPatch();

    // In response to a button click:
    FileNameExtensionFilter filter = new FileNameExtensionFilter("SpinCAD Files", "spcd");
    fc.setFileFilter(filter);
    fc.setAccessory(recentPatchFileList);
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);

    int returnVal = fc.showOpenDialog(new JFrame());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        String filePath = null;
        File file = fc.getSelectedFile();

        System.out.println("Opening: " + file.getName() + "." + newline);
        filePath = file.getPath();
        try {
            p = fileReadPatch(filePath);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("Trying version 952 format..." + newline);
                p = fileReadPatch952(filePath);
            } catch (Exception exc) {
                exc.printStackTrace();
            }
            SpinCADDialogs.MessageBox("File open failed!",
                    "This spcd file may be from\nan incompatible version of \nSpinCAD Designer.");
        } finally {
            saveMRUPatchFolder(filePath);
            recentPatchFileList.add(file);
            String fileName = file.getName();
            p.patchFileName = fileName;
            p.cb.setFileName(fileName);
        }
    } else {
        System.out.println("Open command cancelled by user." + newline);
    }
    saveRecentPatchFileList();
    return p;
}

From source file:com.itemanalysis.jmetrik.swing.GraphPanel.java

public void addJpgMenuItem(final Component parent, JPopupMenu popMenu) {
    JMenuItem mItem = new JMenuItem("Save as JPG...");
    mItem.addActionListener(new ActionListener() {
        @Override/*www . j  av  a 2s.  c o  m*/
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            FileFilter filter1 = new SimpleFilter("jpg", "JPG File (*.jpg)");
            chooser.addChoosableFileFilter(filter1);
            int status = chooser.showSaveDialog(parent);
            if (status == JFileChooser.APPROVE_OPTION) {
                File f = chooser.getSelectedFile();
                try {
                    String fileName = f.getAbsolutePath().toLowerCase();
                    if (!fileName.endsWith("jpg"))
                        fileName += ".jpg";
                    saveAsJPEG(new File(fileName));
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(parent, "IOException: Could not save file", "IOException",
                            JOptionPane.ERROR_MESSAGE);
                }

            }

        }
    });
    popMenu.add(mItem);
}

From source file:item.analysis.report.MainFrame.java

private void browseButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButton1ActionPerformed
    int returnVal = fileChooser1.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser1.getSelectedFile();
        filenameText1.setText(file.getAbsolutePath());
    } else {// w  w  w  . j a  v  a  2s .  c o  m
        System.out.println("File access cancelled by user.");
    }
}