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(FileSystemView fsv) 

Source Link

Document

Constructs a JFileChooser using the given FileSystemView.

Usage

From source file:com.streamhub.StreamHubLicenseGenerator.java

private JPanel createActionsRow() {
    JPanel actionsRow = new JPanel(new FlowLayout());
    File baseFolder = new File(DEFAULT_CUSTOMERS_DIRECTORY);
    JButton generate = new JButton("Generate");
    generate.addActionListener(new ActionListener() {
        @Override//  w  ww.j  av  a2  s  .  c o  m
        public void actionPerformed(ActionEvent e) {
            for (JPanel row : macAddressPanels) {
                try {
                    String macAddress = ((JTextField) row.getComponent(1)).getText();
                    if (macAddress.length() > 0) {
                        String startDate = ((JTextField) row.getComponent(3)).getText();
                        String expiryDate = ((JTextField) row.getComponent(5)).getText();
                        String edition = ((JComboBox) row.getComponent(6)).getSelectedItem().toString();
                        macAddress = macAddress.replaceAll("-", ":").trim();
                        String name = ((JTextField) row.getComponent(8)).getText().trim();
                        String numUsers = ((JTextField) row.getComponent(10)).getText();
                        String licenseString = startDate + "-" + expiryDate + "-" + numUsers + "-" + macAddress
                                + "-" + edition + ":" + name;
                        String hashInput = licenseString + USELESS_KEY;
                        MessageDigest m = MessageDigest.getInstance("SHA-512");
                        m.update(hashInput.getBytes(), 0, hashInput.length());
                        String hash = "==" + new BigInteger(1, m.digest()).toString(16) + "==";
                        StringBuilder licenseText = new StringBuilder();
                        licenseText.append("--").append(licenseString).append("--");
                        licenseText.append(CRLF);
                        licenseText.append(hash);
                        licenseText.append(CRLF);
                        System.out.println(name + ":");
                        System.out.println();
                        System.out.println(licenseText);

                        File licenseDir = new File(folderChooser.getSelectedFile(), name);
                        if (!licenseDir.isDirectory() && !licenseDir.exists()) {
                            licenseDir.mkdir();
                        }
                        File licenseFile = new File(licenseDir, "license.txt");
                        System.out.println("writing to " + licenseFile.getAbsolutePath());
                        FileUtils.writeStringToFile(licenseFile, licenseText.toString());
                    }
                } catch (Exception exception) {
                    System.out.println("Could not generate license");
                    exception.printStackTrace();
                }
            }
        }
    });
    final JButton chooseFolder = new JButton("Choose Folder");
    final JTextField folderDisplay = new JTextField(baseFolder.getAbsolutePath());
    folderChooser = new JFileChooser(baseFolder);
    folderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    folderChooser.setSelectedFile(baseFolder);
    chooseFolder.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == chooseFolder) {
                int returnVal = folderChooser.showOpenDialog(StreamHubLicenseGenerator.this);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File folder = folderChooser.getSelectedFile();
                    folderDisplay.setText(folder.getAbsolutePath());
                }
            }
        }
    });

    actionsRow.add(folderDisplay);
    actionsRow.add(chooseFolder);
    actionsRow.add(generate);
    return actionsRow;
}

From source file:CompareFiles.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 w w .j  av a 2s .  c o  m*/
    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);
        jLabel2.setText(myfolder.toString());
    }
}

From source file:modelibra.swing.app.util.FileSelector.java

/**
 * Selects a directory (path)./*  ww w .j  a v  a 2 s  . c  o  m*/
 * 
 * @param lang
 *            language
 * @return chosen directory path
 */
public String selectDirectory(NatLang lang) {
    JFileChooser dirChooser = new JFileChooser(lastDirectoryPath);
    dirChooser.setLocale(lang.getLocale());
    dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    dirChooser.setToolTipText(lang.getText("selectDirectory"));
    dirChooser.setDialogTitle(lang.getText("selectDirectory"));
    dirChooser.showDialog(null, lang.getText("selectDirectory"));
    File f = dirChooser.getSelectedFile();
    if (f != null) {
        lastDirectoryPath = f.getPath();
        return lastDirectoryPath;
    } else {
        return null;
    }
}

From source file:de.codesourcery.jasm16.ide.ui.views.EmulationOptionsView.java

public EmulationOptionsView() {
    emulatorPanel.setLayout(new GridBagLayout());

    GridBagConstraints cnstrs = constraints(0, 0, false, false, GridBagConstraints.NONE);
    emulatorPanel.add(new JLabel("Emulation speed"), cnstrs);

    cnstrs = constraints(1, 0, true, true, GridBagConstraints.NONE);
    cnstrs.anchor = GridBagConstraints.WEST;
    emulatorPanel.setBorder(BorderFactory.createTitledBorder("General options"));

    speedBox.setRenderer(new DefaultListCellRenderer() {

        public Component getListCellRendererComponent(javax.swing.JList<?> list, Object value, int index,
                boolean isSelected, boolean cellHasFocus) {
            final java.awt.Component result = super.getListCellRendererComponent(list, value, index, isSelected,
                    cellHasFocus);// w  ww  .ja  v a  2 s.co  m

            if (value != null) {
                switch ((EmulationSpeed) value) {
                case MAX_SPEED:
                    setText("Max.");
                    break;
                case REAL_SPEED:
                    setText("100 kHz");
                    break;
                default:
                    setText(value.toString());
                    break;
                }
            }
            return result;
        };
    });

    emulatorPanel.add(speedBox, cnstrs);

    // disk drive panel
    selectedFileField.setColumns(25);

    diskDrivePanel.setLayout(new GridBagLayout());
    cnstrs = constraints(0, 0, false, true, GridBagConstraints.NONE);
    cnstrs.anchor = GridBagConstraints.CENTER;

    diskDrivePanel.setBorder(BorderFactory.createTitledBorder("Disk drive"));
    diskDrivePanel.add(selectedFileField, cnstrs);

    cnstrs = constraints(1, 0, false, true, GridBagConstraints.NONE);
    cnstrs.anchor = GridBagConstraints.CENTER;
    diskDrivePanel.add(fileChooserButton, cnstrs);

    fileChooserButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final JFileChooser chooser;
            if (getSelectedFile() != null) {
                chooser = new JFileChooser(getSelectedFile().getParentFile());
            } else {
                chooser = new JFileChooser();
            }
            final int result = chooser.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION && chooser.getSelectedFile().isFile()) {
                selectedFileField.setText(chooser.getSelectedFile().getAbsolutePath());
            }
        }
    });

    cnstrs = constraints(2, 0, false, true, GridBagConstraints.NONE);
    cnstrs.anchor = GridBagConstraints.CENTER;
    diskDrivePanel.add(writeProtected, cnstrs);
}

From source file:storybook.ui.chart.AbstractChartPanel.java

private AbstractAction getExportAction() {
    if (this.exportAction == null) {
        this.exportAction = new AbstractAction() {
            @Override//  w  w  w . j  ava 2s . c  om
            public void actionPerformed(ActionEvent paramAnonymousActionEvent) {
                try {
                    Internal localInternal = BookUtil.get(AbstractChartPanel.this.mainFrame,
                            SbConstants.BookKey.EXPORT_DIRECTORY,
                            EnvUtil.getDefaultExportDir(AbstractChartPanel.this.mainFrame));
                    File localFile1 = new File(localInternal.getStringValue());
                    JFileChooser localJFileChooser = new JFileChooser(localFile1);
                    localJFileChooser.setFileFilter(new PngFileFilter());
                    localJFileChooser.setApproveButtonText(I18N.getMsg("msg.common.export"));
                    String str = AbstractChartPanel.this.mainFrame.getDbFile().getName() + " - "
                            + AbstractChartPanel.this.chartTitle;
                    str = IOUtil.cleanupFilename(str);
                    localJFileChooser.setSelectedFile(new File(str));
                    int i = localJFileChooser.showDialog(AbstractChartPanel.this.getThis(),
                            I18N.getMsg("msg.common.export"));
                    if (i == 1) {
                        return;
                    }
                    File localFile2 = localJFileChooser.getSelectedFile();
                    if (!localFile2.getName().endsWith(".png")) {
                        localFile2 = new File(localFile2.getPath() + ".png");
                    }
                    ScreenImage.createImage(AbstractChartPanel.this.panel, localFile2.toString());
                    JOptionPane.showMessageDialog(AbstractChartPanel.this.getThis(),
                            I18N.getMsg("msg.common.export.success"), I18N.getMsg("msg.common.export"), 1);
                } catch (HeadlessException | IOException localException) {
                }
            }
        };
    }
    return this.exportAction;
}

From source file:com.naval.gui.Gui.java

private void chargerPartie() {
    final JFileChooser fc = new JFileChooser(Config.getRepTravail());

    fc.addChoosableFileFilter(new GameFileFilter("serial", "Sauv de partie"));
    fc.setAcceptAllFileFilterUsed(false);

    int returnVal = fc.showOpenDialog(frame);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();

        // TODO : load only if game is !=
        try {//  ww w  . j  av a  2  s  .co m
            partie = Partie.load(file.getAbsolutePath());

            menuFac.updateForLoad();
            update();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            hintBar.setText(e.getMessage());
        } catch (IOException e) {
            hintBar.setText(e.getMessage());
            e.printStackTrace();

        } catch (ClassNotFoundException e) {
            hintBar.setText(e.getMessage());
            e.printStackTrace();
        }
    }
    if (partie.ordres == null || partie.ordres.size() == 0) {
        for (Navire n : partie.navires) {
            // creation des 3 ordres pour le tour courant.
            partie.ordres.add(new Ordre(n.id, partie.minute));
            partie.ordres.add(new Ordre(n.id, partie.minute));
            partie.ordres.add(new Ordre(n.id, partie.minute));

        }
    }
}

From source file:modelibra.swing.app.util.FileSelector.java

/**
 * Selects a file (path).// w  ww .  j a v  a2s.c o m
 * 
 * @param lang
 *            language
 * @return chosen file path
 */
public String selectFile(NatLang lang) {
    JFileChooser fileChooser = new JFileChooser(lastFilePath);
    fileChooser.setLocale(lang.getLocale());
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fileChooser.setToolTipText(lang.getText("selectFile"));
    fileChooser.setDialogTitle(lang.getText("selectFile"));
    fileChooser.showDialog(null, lang.getText("selectFile"));
    File f = fileChooser.getSelectedFile();
    if (f != null) {
        lastFilePath = f.getPath();
        return lastFilePath;
    } else {
        return null;
    }
}

From source file:grafix.principal.Comandos.java

static public void cmdSalvarConfiguracao() {
    ControleRegistro.alertaRegistro();//from  w w w .  ja v  a  2 s. co m
    JFileChooser chooser = new JFileChooser(new File(ConfiguracoesGrafix.PASTA_TEMPLATES));
    chooser.setSelectedFile(new File(ConfiguracoesGrafix.EXTENSAO_TEMPLATES));
    chooser.setDialogType(JFileChooser.SAVE_DIALOG);
    int returnVal = chooser.showSaveDialog(Controle.getTela());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        try {
            Controle.getConfiguracoesUsuario().setNome(file.getName());
            Controle.salvarConfiguracoesUsuario(true);
            LeitorArquivoConfiguracao.getInstance().criarCopia(file);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    Controle.getTela().getComboConfiguracoes().popularCombo();
}

From source file:de.codesourcery.jasm16.ide.ui.views.ProjectConfigurationView.java

@Override
protected JPanel getPanel() {
    final JPanel result = new JPanel();
    result.setLayout(new GridBagLayout());

    // project name
    int y = 0;/*from w w w  . ja  v  a 2 s. com*/
    GridBagConstraints cnstrs = constraints(0, y, false, false, GridBagConstraints.NONE);
    result.add(new JLabel("Project name"), cnstrs);

    cnstrs = constraints(1, y++, true, false, GridBagConstraints.NONE);
    result.add(projectName, cnstrs);

    // build options panel
    final JPanel buildOptionsPanel = new JPanel();
    buildOptionsPanel.setLayout(new GridBagLayout());

    buildOptionsPanel.setBorder(BorderFactory.createTitledBorder("Build options"));

    cnstrs = constraints(0, 0, false, false, GridBagConstraints.NONE);
    buildOptionsPanel.add(new JLabel("Compilation root"), cnstrs);

    cnstrs = constraints(1, 0, false, false, GridBagConstraints.NONE);
    compilationRootName.setEditable(false);
    compilationRootName.setColumns(25);
    buildOptionsPanel.add(compilationRootName, cnstrs);

    cnstrs = constraints(2, 0, true, false, GridBagConstraints.NONE);
    buildOptionsPanel.add(compilationRootButton, cnstrs);

    compilationRootButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final JFileChooser chooser;
            File baseDir = null;
            if (project != null) {
                baseDir = project.getConfiguration().getBaseDirectory();
            }
            if (StringUtils.isNotBlank(compilationRootName.getText())) {
                File tmp = new File(compilationRootName.getText()).getParentFile();
                if (tmp.exists() && tmp.isDirectory()) {
                    baseDir = tmp;
                }
            }
            if (baseDir != null) {
                chooser = new JFileChooser(baseDir);
            } else {
                chooser = new JFileChooser();
            }
            final int result = chooser.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION && chooser.getSelectedFile().isFile()) {
                compilationRootName.setText(chooser.getSelectedFile().getAbsolutePath());
            }
        }
    });

    // generate self-relocating code ?
    cnstrs = constraints(0, 1, false, false, GridBagConstraints.NONE);
    buildOptionsPanel.add(new JLabel("Generate self-relocating code?"), cnstrs);

    cnstrs = constraints(1, 1, true, true, GridBagConstraints.NONE);
    cnstrs.gridwidth = 2;
    buildOptionsPanel.add(generateSelfRelocatingCode, cnstrs);

    // inline short literals ?
    cnstrs = constraints(0, 2, false, false, GridBagConstraints.NONE);
    buildOptionsPanel.add(new JLabel("Inline short literals?"), cnstrs);

    cnstrs = constraints(1, 2, true, true, GridBagConstraints.NONE);
    cnstrs.gridwidth = 2;
    buildOptionsPanel.add(inlineShortLiterals, cnstrs);

    // add build options panel to parent
    cnstrs = constraints(0, y++, true, false, GridBagConstraints.BOTH);
    cnstrs.gridwidth = 2;
    result.add(buildOptionsPanel, cnstrs);

    // buttons
    final JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridBagLayout());

    cnstrs = constraints(0, 0, false, false, GridBagConstraints.NONE);
    buttonPanel.add(saveButton, cnstrs);
    saveButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (hasValidInput()) {
                onSave();
            }
        }
    });

    cnstrs = constraints(1, 0, true, true, GridBagConstraints.NONE);
    buttonPanel.add(cancelButton, cnstrs);
    cancelButton.addActionListener(new ActionListener() {

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

    // button panel
    cnstrs = constraints(0, y++, true, true, GridBagConstraints.NONE);
    cnstrs.gridwidth = 2;
    result.add(buttonPanel, cnstrs);

    return result;
}

From source file:CompareFiles.java

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

    String userDir = System.getProperty("user.home");
    JFileChooser folder = new JFileChooser(userDir + "/Desktop");
    folder.setFileSelectionMode(JFileChooser.FILES_ONLY);
    FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter("Excel Files  (*.xls)", "xls");
    folder.setFileFilter(xmlfilter);//from   w  w w .  j  a v a 2s .  co m
    //        folder.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    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);
        jLabel1.setText(myfolder.toString());
    }

}