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:ca.canucksoftware.ipkpackager.IpkPackagerView.java

private File loadFileChooser(boolean dir, FileFilter filter, String text) {
    File result = null;//from   w ww. java 2  s.c o  m
    JFileChooser fc = new JFileChooser(); //Create a file chooser
    disableNewFolderButton(fc);
    if (text != null) {
        fc.setSelectedFile(new File(text));
    }
    if (dir) {
        fc.setDialogTitle("");
        File lastDir = new File(
                Preferences.userRoot().get("lastDir", fc.getCurrentDirectory().getAbsolutePath()));
        fc.setCurrentDirectory(lastDir);
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (fc.showDialog(null, "Select") == JFileChooser.APPROVE_OPTION) {
            result = fc.getSelectedFile();
            jTextField1.setText(result.getAbsolutePath());
            Preferences.userRoot().put("lastDir", result.getParentFile().getAbsolutePath());
        }
    } else {
        File lastSaved = null;
        File lastSelected = null;
        if (filter != null) {
            fc.setDialogTitle("Save As...");
            lastSaved = new File(
                    Preferences.userRoot().get("lastSaved", fc.getCurrentDirectory().getAbsolutePath()));
            fc.setCurrentDirectory(lastSaved);
            fc.setFileFilter(filter);
        } else {
            fc.setDialogTitle("");
            lastSelected = new File(
                    Preferences.userRoot().get("lastSelected", fc.getCurrentDirectory().getAbsolutePath()));
            fc.setCurrentDirectory(lastSelected);
            fc.setAcceptAllFileFilterUsed(true);
        }
        if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
            result = fc.getSelectedFile();
            if (lastSaved != null) {
                Preferences.userRoot().put("lastSaved", result.getParentFile().getAbsolutePath());
            }
            if (lastSelected != null) {
                Preferences.userRoot().put("lastSelected", result.getParentFile().getAbsolutePath());
            }
        }
    }
    return result;
}

From source file:ja.lingo.application.gui.main.export.ExportGui.java

private String askFile(JaFilter filter, String fileName, String dialogTitle) {
    chooser.setDialogTitle(dialogTitle);
    chooser.resetChoosableFileFilters();
    chooser.setFileFilter(filter);/*from   www  . j a  va2  s.c  o  m*/
    chooser.setSelectedFile(
            new File(filter.appendExtensionIfNeeded(Files.removeUnacceptableSymbols(fileName))));

    if (chooser.showSaveDialog(parentFrame) == JFileChooser.APPROVE_OPTION) {
        return filter.appendExtensionIfNeeded(chooser.getSelectedFile().getAbsolutePath());
    }
    return null;
}

From source file:FTPApp.java

public FTPApp() {
    super("FTP Client");

    JPanel p = new JPanel();
    p.setBorder(new EmptyBorder(5, 5, 5, 5));

    p.add(new JLabel("User name:"));
    p.add(userNameTextField);// w w w .  j a va 2  s  .c  o  m
    p.add(new JLabel("Password:"));
    p.add(passwordTextField);
    p.add(new JLabel("URL:"));
    p.add(urlTextField);
    p.add(new JLabel("File:"));
    p.add(fileTextField);

    monitorTextArea.setEditable(false);
    JScrollPane ps = new JScrollPane(monitorTextArea);
    p.add(ps);

    m_progress.setStringPainted(true);
    m_progress.setBorder(new BevelBorder(BevelBorder.LOWERED, Color.white, Color.gray));
    m_progress.setMinimum(0);
    JPanel p1 = new JPanel(new BorderLayout());
    p1.add(m_progress, BorderLayout.CENTER);
    p.add(p1);

    ActionListener lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (connect()) {
                Thread uploader = new Thread() {
                    public void run() {
                        putFile();
                        disconnect();
                    }
                };
                uploader.start();
            }
        }
    };
    putButton.addActionListener(lst);
    putButton.setMnemonic('p');
    p.add(putButton);

    getButton = new JButton("Get");
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (connect()) {
                Thread downloader = new Thread() {
                    public void run() {
                        getFile();
                        disconnect();
                    }
                };
                downloader.start();
            }
        }
    };
    getButton.addActionListener(lst);
    getButton.setMnemonic('g');
    p.add(getButton);

    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (fileChooser.showSaveDialog(FTPApp.this) != JFileChooser.APPROVE_OPTION)
                return;
            File f = fileChooser.getSelectedFile();
            fileTextField.setText(f.getPath());
        }
    };
    fileButton.addActionListener(lst);
    fileButton.setMnemonic('f');
    p.add(fileButton);

    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (ftpClient != null)
                disconnect();
            else
                System.exit(0);
        }
    };
    closeButton.addActionListener(lst);
    closeButton.setDefaultCapable(true);
    closeButton.setMnemonic('g');
    p.add(closeButton);

    getContentPane().add(p, BorderLayout.CENTER);

    fileChooser.setCurrentDirectory(new File("."));
    fileChooser.setApproveButtonToolTipText("Select file for upload/download");

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            disconnect();
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);

    setSize(720, 240);
    setVisible(true);
}

From source file:EnhancedFileTester.java

public EnhancedFileTester() {
    JButton jb = new JButton("Open File Viewer");
    add(jb);//from   w ww  . ja  v  a  2s.  c  o  m
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser(".");
            FileFilter type1 = new ExtensionFilter("Java source", ".java");
            FileFilter type2 = new ExtensionFilter("Image files",
                    new String[] { ".jpg", ".gif", "jpeg", "xbm" });
            FileFilter type3 = new ExtensionFilter("HTML files", new String[] { ".htm", ".html" });
            chooser.addChoosableFileFilter(type1);
            chooser.addChoosableFileFilter(type2);
            chooser.addChoosableFileFilter(type3);
            chooser.setFileFilter(type2); // Initial filter setting
            FileView view = new IconView();
            chooser.setFileView(view);
            int status = chooser.showOpenDialog(EnhancedFileTester.this);
            if (status == JFileChooser.APPROVE_OPTION) {
                File f = chooser.getSelectedFile();
                System.out.println(f);
            }
        }
    };
    jb.addActionListener(listener);
}

From source file:net.aepik.alasca.gui.util.LoadFileFrame.java

/**
 * Perform action on event for this object.
 *//*from w w w  . jav a 2s.com*/
public void actionPerformed(ActionEvent e) {
    Object o = e.getSource();
    if (o == boutonOpenFile) {
        JFileChooser jfcProgramme = new JFileChooser(".");
        jfcProgramme.setMultiSelectionEnabled(false);
        jfcProgramme.setDialogTitle("Selectionner un fichier");
        jfcProgramme.setApproveButtonText("Selectionner");
        jfcProgramme.setApproveButtonToolTipText("Cliquer apres avoir selectionn un fichier");
        jfcProgramme.setAcceptAllFileFilterUsed(false);
        if (jfcProgramme.showDialog(this, null) == JFileChooser.APPROVE_OPTION) {
            try {
                filename.setText(jfcProgramme.getSelectedFile().getCanonicalPath());
            } catch (IOException ioe) {
                JOptionPane.showMessageDialog(null, "Erreur de nom de fichier.", "Erreur",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    if (o == boutonOk && filename.getText().length() != 0) {
        if (!this.loadFile(filename.getText(), (String) this.syntaxes.getSelectedItem())) {
            JOptionPane.showMessageDialog(this, this.getErrorMessage(), "Erreur", JOptionPane.ERROR_MESSAGE);
        } else {
            windowClosing(null);
        }
    }
    if (o == boutonAnnuler) {
        windowClosing(null);
    }
}

From source file:cmsc105_mp2.Plot.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    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  va  2s  .c  o m*/
            ImageIO.write(bImage1, "png", new File(chooser.getSelectedFile() + ".png"));
        } catch (IOException ex) {
            System.out.println("Unable to Print!");
        }
    }
}

From source file:mineria.UI.java

public UI() {
    this.setLayout(new GridBagLayout());

    Label lblnodatos = new Label("NoDatos: ");
    Label label = new Label("BD: ");
    JTextField filename = new JTextField("/Users/eduardomartinez/Documents/mineria/representacion.txt");
    JTextField nodatos = new JTextField();
    nodatos.setText("500");

    JTextField funcion = new JTextField("6");
    JTextField individuos = new JTextField("200");
    JTextField enteros = new JTextField("1");
    JTextField decimales = new JTextField("40");
    JTextField variables = new JTextField("6");
    JTextField Pc = new JTextField("0.9");
    JTextField Pm = new JTextField("0.01");
    JTextField generaciones = new JTextField("100");
    JTextField minimiza = new JTextField("0");

    Label lblfuncion = new Label("funcion: ");
    Label lblindividuos = new Label("individuos: ");
    Label lblenteros = new Label("enteros: ");
    Label lbldecimales = new Label("decimales: ");
    Label lblvariables = new Label("variables: ");
    Label lblpc = new Label("Pc: ");
    Label lblpm = new Label("Pm: ");
    Label lblgeneraciones = new Label("generaciones: ");
    Label lblminimiza = new Label("[0 Min/1 Max] : ");

    /*/*from   w  w w.j a  va  2 s  .com*/
    FN=funcion;
    N =individuos;
    E =bits_enteros;
    D =bits_decimales;
    V =variables;
    Pc=porcentaje_cruza;
    Pm=porcentaje_muta;
    G =generaciones;
    MM=minimiza;
    */

    JButton openBtn = new JButton("Open BD");
    JButton ejecutarEGA = new JButton("Ejecutar EGA");
    JTextField resultado = new JTextField();
    Label fitness = new Label("fitness: ");

    XYSeriesCollection datosSerie = new XYSeriesCollection();

    AGF agf = new AGF(2);
    EGA ega = new EGA();

    JFreeChart chart = ChartFactory.createScatterPlot("Scatter Plot", // chart title
            "X", // x axis label
            "Y", // y axis label
            datosSerie, // data  ***-----PROBLEM------***
            PlotOrientation.VERTICAL, true, // include legend
            true, // tooltips
            false // urls
    );

    // create and display a frame...
    ChartPanel panelChart = new ChartPanel(chart);

    //leer BD
    openBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            //datosSerie.removeAllSeries();
            if (filename.getText().length() > 0) {
                agf.LeerDatos(filename.getText(), Integer.parseInt(nodatos.getText()));

                createDataset(datosSerie, agf.data, "Datos");
                chart.fireChartChanged();
            } else {
                JFileChooser openFile = new JFileChooser();
                int rVal = openFile.showOpenDialog(null);
                if (rVal == JFileChooser.APPROVE_OPTION) {
                    filename.setText(openFile.getSelectedFile().getAbsolutePath());
                    agf.LeerDatos(filename.getText(), Integer.parseInt(nodatos.getText()));
                    //createDataset(datosSerie, agf.data, "Datos");
                    //chart.fireChartChanged();
                }
                if (rVal == JFileChooser.CANCEL_OPTION) {
                    filename.setText("");
                    //dir.setText("");
                }
            }
        }
    });

    ejecutarEGA.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            //datosSerie.removeAllSeries();
            if (datosSerie.getSeriesCount() > 1)
                datosSerie.removeSeries(1);

            int fn = Integer.parseInt(funcion.getText());
            int n = Integer.parseInt(individuos.getText());
            int e = Integer.parseInt(enteros.getText());
            int d = Integer.parseInt(decimales.getText());
            int v = Integer.parseInt(variables.getText());
            double pc = Double.parseDouble(Pc.getText());
            double pm = Double.parseDouble(Pm.getText());
            int g = Integer.parseInt(generaciones.getText());
            int mm = Integer.parseInt(minimiza.getText());

            ega.setParams(fn, n, e, d, v, pc, pm, g, mm);

            Resultado res = ega.ejecutarAlgoritmoGenetico(agf);

            resultado.setText(String.valueOf(res.getFitnessSemental()));

            res.creaArchivo();
            createDataset(datosSerie, res.getFenotipoSemental(), "Centros");

            Shape cross = ShapeUtilities.createDiagonalCross(5, 1);
            XYPlot xyPlot = (XYPlot) chart.getPlot();
            xyPlot.setDomainCrosshairVisible(true);
            xyPlot.setRangeCrosshairVisible(true);
            XYItemRenderer renderer = xyPlot.getRenderer();
            renderer.setSeriesShape(1, cross);
            renderer.setSeriesPaint(1, Color.blue);

            chart.fireChartChanged();

        }
    });
    //ejecutar AG

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 2;
    gbc.gridy = 0;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(lblnodatos, gbc);

    gbc.gridx = 3;
    gbc.gridy = 0;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(nodatos, gbc);

    gbc.gridx = 2;
    gbc.gridy = 1;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(filename, gbc);

    gbc.gridx = 3;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(openBtn, gbc);

    gbc.gridx = 2;
    gbc.gridy = 2;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(fitness, gbc);

    gbc.gridx = 3;
    gbc.gridy = 2;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(resultado, gbc);

    gbc.gridx = 2;
    gbc.gridy = 3;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(ejecutarEGA, gbc);

    //-----------------PARAMETROS
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(lblfuncion, gbc);

    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(funcion, gbc);

    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(lblindividuos, gbc);

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(individuos, gbc);

    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(lblenteros, gbc);

    gbc.gridx = 1;
    gbc.gridy = 2;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(enteros, gbc);

    gbc.gridx = 0;
    gbc.gridy = 3;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(lbldecimales, gbc);

    gbc.gridx = 1;
    gbc.gridy = 3;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(decimales, gbc);

    gbc.gridx = 0;
    gbc.gridy = 4;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(lblvariables, gbc);

    gbc.gridx = 1;
    gbc.gridy = 4;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(variables, gbc);

    gbc.gridx = 0;
    gbc.gridy = 5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(lblpc, gbc);

    gbc.gridx = 1;
    gbc.gridy = 5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(Pc, gbc);

    gbc.gridx = 0;
    gbc.gridy = 6;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(lblpm, gbc);

    gbc.gridx = 1;
    gbc.gridy = 6;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(Pm, gbc);

    gbc.gridx = 0;
    gbc.gridy = 7;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(lblgeneraciones, gbc);

    gbc.gridx = 1;
    gbc.gridy = 7;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(generaciones, gbc);

    gbc.gridx = 0;
    gbc.gridy = 8;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(lblminimiza, gbc);

    gbc.gridx = 1;
    gbc.gridy = 8;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(minimiza, gbc);

    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridx = 0;
    gbc.gridy = 9;
    gbc.gridheight = 2;
    gbc.gridwidth = 4;
    this.add(panelChart, gbc);

    this.setTitle("File Chooser");

    this.pack();
}

From source file:com.db4o.sync4o.ui.Db4oSyncSourceConfigPanel.java

/**
 * Default constructor is required by Funambol Admin UI
 *//*from  w w  w .jav  a  2  s.c  om*/
public Db4oSyncSourceConfigPanel() {

    setupControls();

    // now we install our event handlers and we are ready to go...

    // handler to update the SyncClassConfigTree upon a change 
    // in database file
    _dbFileLocateButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            try {

                final JFileChooser fc = new JFileChooser();
                int rc = fc.showOpenDialog(Db4oSyncSourceConfigPanel.this);
                if (JFileChooser.APPROVE_OPTION == rc) {

                    File f = fc.getSelectedFile();
                    _dbFileValue.setText(f.getPath());
                    Db4oSyncSourceConfigPanel.this.refreshClassConfigsFromFile();

                }

            } catch (Exception ex) {

                StringWriter s = new StringWriter();
                PrintWriter w = new PrintWriter(s);

                ex.printStackTrace(w);
                notifyError(new AdminException(s.toString()));

            }

        }

    });

    // handler for the "Add" or "Update" button
    _confirmButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {

            try {
                validateValues();
                updateSyncSource();

                if (getState() == STATE_INSERT) {
                    Db4oSyncSourceConfigPanel.this.actionPerformed(new ActionEvent(
                            Db4oSyncSourceConfigPanel.this, ACTION_EVENT_INSERT, event.getActionCommand()));
                } else {
                    Db4oSyncSourceConfigPanel.this.actionPerformed(new ActionEvent(
                            Db4oSyncSourceConfigPanel.this, ACTION_EVENT_UPDATE, event.getActionCommand()));
                }
            } catch (Exception e) {
                notifyError(new AdminException(e.getMessage()));
            }
        }
    });

}

From source file:it.unibas.spicygui.controllo.mapping.ActionExecuteXQuery.java

private File generateFile() {
    JFileChooser chooser = vista.getFileChooserSalvaXmlXsd();
    boolean continua = true;
    File file;/* w  w w  .  j  av a  2 s  .  com*/
    while (continua) {
        int returnVal = chooser.showSaveDialog(WindowManager.getDefault().getMainWindow());
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
            if (!file.exists() || chiediConferma()) {
                return file;
            }
        } else {
            continua = false;
        }
    }
    return null;
}

From source file:be.fedict.eid.tsl.tool.SignSelectPkcs11FinishablePanel.java

@Override
public void actionPerformed(ActionEvent e) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setDialogTitle("Select PKCS#11 library");
    int returnValue = fileChooser.showOpenDialog(null);
    if (JFileChooser.APPROVE_OPTION == returnValue) {
        String pkcs11Library = fileChooser.getSelectedFile().getAbsolutePath();
        this.pkcs11TextField.setText(pkcs11Library);
    }//  w w w  .j a v a2 s. co m
}