Example usage for javax.swing JFileChooser getSelectedFile

List of usage examples for javax.swing JFileChooser getSelectedFile

Introduction

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

Prototype

public File getSelectedFile() 

Source Link

Document

Returns the selected file.

Usage

From source file:com.itd.dbmrgdao.TestTime6.java

@Ignore
@org.junit.Test//from   w  w  w.j av a2 s.  co  m
public void test() throws ParseException {
    String newTab = "\t";
    String[] sysidLine;

    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Text File", "txt");
    chooser.setFileFilter(filter);
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();

    String fileName = f.getAbsolutePath();
    try {
        FileReader reader = new FileReader(fileName);
        BufferedReader br = new BufferedReader(reader);
        String strLine;

        String sysid = null, scdate = null, p1Start = null, p1End = null, p2Start = null, p2End = null,
                otStart = null, otEnd = null;
        String scode = "1985";
        StringBuilder mySql = new StringBuilder(
                "INSERT INTO scandata(sc_sysid,sc_scode,scdate,p1start,p2end) VALUES ");

        //             StringBuilder mySql = new StringBuilder(
        //                    "INSERT INTO scandata(sc_sysid,sc_scode,scdate,p1start,p1end,p2start,p2end,otstart,otend) VALUES ");

        // read from select file
        while ((strLine = br.readLine()) != null) {
            String[] parts = strLine.split(" ");

            // check if not first record or sysid change will save in table scandata 
            if (sysid != null && !sysid.equals(parts[0])) {
                mySql = mySql.append(" ('").append(sysid).append("','").append(scode).append("','")
                        .append(scdate).append("','").append(p1Start).append("','").append(p2End).append("'),");

                p1Start = null;
                p1End = null;
                p2Start = null;
                p2End = null;
                otStart = null;
                otEnd = null;

            }

            //clear 
            sysid = parts[0];
            scdate = new StringBuffer(parts[1]).insert(parts[1].length() - 2, "-").toString();
            scdate = new StringBuffer(scdate).insert(scdate.length() - 5, "-").toString();

            String timeString = new StringBuffer(parts[5]).insert(parts[5].length() - 2, ":").toString();

            if (parts[6].equals("01")) {
                p1Start = CompareTime(p1Start, timeString, "01");
            } else {
                p2End = CompareTime(p2End, timeString, "04");
            }
        }

        mySql = mySql.append(" ('").append(sysid).append("','").append(scode).append("','").append(scdate)
                .append("','").append(p1Start).append("','").append(p2End).append("')");

        scanDao.executeSQL(mySql.toString());

        br.close();

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }

}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopExportDisplay.java

private void saveFileAction(String fileName, JFrame frame, ExportDataProvider dataProvider) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setSelectedFile(new File(fileName));
    if (fileChooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        boolean success = saveFile(dataProvider, selectedFile);
        TopLevelFrame mainFrame = App.getInstance().getMainFrame();
        if (success) {
            mainFrame.showNotification(messages.getMessage(DesktopExportDisplay.class, "export.saveSuccess"),
                    Frame.NotificationType.TRAY);
        } else {/* ww w .  j  ava  2  s  .co m*/
            mainFrame.showNotification(messages.getMessage(DesktopExportDisplay.class, "export.saveError"),
                    Frame.NotificationType.ERROR);
        }
    }
}

From source file:net.rptools.maptool.client.macro.impl.SaveAliasesMacro.java

public void execute(MacroContext context, String macro, MapToolMacroContext executionContext) {
    File aliasFile = null;/*ww w .  j av  a2  s. com*/
    if (macro.length() > 0) {
        aliasFile = new File(macro);
    } else {
        JFileChooser chooser = MapTool.getFrame().getSaveFileChooser();
        chooser.setDialogTitle("savealiases.dialogTitle");
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

        if (chooser.showOpenDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
            return;
        }
        aliasFile = chooser.getSelectedFile();
    }
    if (aliasFile.getName().indexOf(".") < 0) {
        aliasFile = new File(aliasFile.getAbsolutePath() + ".alias");
    }
    if (aliasFile.exists() && !MapTool.confirm(I18N.getText("msg.confirm.fileExists"))) {
        return;
    }

    try {
        StringBuilder builder = new StringBuilder();
        builder.append("# ").append(I18N.getText("savealiases.created")).append(" ")
                .append(new SimpleDateFormat().format(new Date())).append("\n\n");

        Map<String, String> aliasMap = MacroManager.getAliasMap();
        List<String> aliasList = new ArrayList<String>();
        aliasList.addAll(aliasMap.keySet());
        Collections.sort(aliasList);
        for (String key : aliasList) {
            String value = aliasMap.get(key);
            builder.append(key).append(":").append(value).append("\n"); // LATER: this character should be externalized and shared with the load alias macro
        }
        FileUtils.writeByteArrayToFile(aliasFile, builder.toString().getBytes("UTF-8"));

        MapTool.addLocalMessage(I18N.getText("aliases.saved"));
    } catch (FileNotFoundException fnfe) {
        MapTool.addLocalMessage(
                I18N.getText("savealiases.couldNotSave", I18N.getText("msg.error.fileNotFound")));
    } catch (IOException ioe) {
        MapTool.addLocalMessage(I18N.getText("savealiases.couldNotSave", ioe));
    }
}

From source file:org.pmedv.jake.commands.OpenPlayerViewCommand.java

@Override
public void execute() {

    final ApplicationContext ctx = AppContext.getApplicationContext();
    final ApplicationWindow win = (ApplicationWindow) ctx.getBean(BeanDirectory.WINDOW_APPLICATION);

    /**//from  w ww  .  j a  v a  2s  .c o m
     * Get last selected folder to simplify file browsing
     */

    if (AppContext.getLastSelectedFolder() == null)
        AppContext.setLastSelectedFolder(System.getProperty("user.home"));

    JFileChooser fc = new JFileChooser(AppContext.getLastSelectedFolder());
    fc.setDialogTitle("Open directory");
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    int result = fc.showOpenDialog(win);

    if (result == JFileChooser.CANCEL_OPTION)
        return;

    AppContext.setLastSelectedFolder(fc.getSelectedFile().getAbsolutePath());

    List<File> files = new ArrayList<File>();
    FileUtils.findFile(files, fc.getSelectedFile(), ".mp3", true, true);

    final PlayerController controller = new PlayerController(files);
    JakeUtil.updateRecentFiles(fc.getSelectedFile().getAbsolutePath());

    View view = new View(fc.getSelectedFile().getAbsolutePath(), null, controller.getPlayerView());

    view.addListener(new DockingWindowAdapter() {

        @Override
        public void windowClosing(DockingWindow arg0) throws OperationAbortedException {
            if (controller.getPlayer() != null)
                controller.getPlayer().close();
            controller.getPlayFileCommand().setPlaying(false);
        }

    });

    openEditor(view);
}

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 . ja va2s.c  om
    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:com.itd.dbmrgdao.TestTime5.java

@Ignore
@org.junit.Test/*w ww .jav a2  s  .co m*/
public void test() throws ParseException {
    String newTab = "\t";
    String[] sysidLine;

    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Text File", "txt");
    chooser.setFileFilter(filter);
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();

    String fileName = f.getAbsolutePath();
    try {
        FileReader reader = new FileReader(fileName);
        BufferedReader br = new BufferedReader(reader);
        String strLine;

        String sysid = null, scdate = null, p1Start = null, p1End = null, p2Start = null, p2End = null,
                otStart = null, otEnd = null;

        // read from select file
        while ((strLine = br.readLine()) != null) {
            String[] parts = strLine.split(" ");

            // check if not first record or sysid change will save in table scandata 
            if (sysid != null && !sysid.equals(parts[0])) {

                List<Object> lst = new ArrayList<Object>();

                scanDao.deleteScanDataBySysId_Sdate(sysid, scdate);
                scanDao.insertScanData(sysid, "1985", scdate, p1Start, p1End, p2Start, p2End, otStart, otEnd);

                p1Start = null;
                p1End = null;
                p2Start = null;
                p2End = null;
                otStart = null;
                otEnd = null;

            }

            //clear 
            sysid = parts[0];
            scdate = new StringBuffer(parts[1]).insert(parts[1].length() - 2, "-").toString();
            scdate = new StringBuffer(scdate).insert(scdate.length() - 5, "-").toString();

            String timeString = new StringBuffer(parts[5]).insert(parts[5].length() - 2, ":").toString();

            if (parts[6].equals("01")) {
                p1Start = CompareTime(p1Start, timeString, "01");
            } else {
                p2End = CompareTime(p2End, timeString, "04");
            }

        }

        scanDao.deleteScanDataBySysId_Sdate(sysid, scdate);
        scanDao.insertScanData(sysid, "1985", scdate, p1Start, p1End, p2Start, p2End, otStart, otEnd);

        br.close();

        //last line
        //            if (p1Start != null || p2End != null) {
        //                ScanRule scanRule = scanDao.findScanRuleBySysId(sysid);
        //                p1Start = scanRule.getP1start();
        //                p2End = scanRule.getP2end();
        //            }
        //            ScanDataKey scanDataKey = new ScanDataKey(sysid, scdate);
        //            ScanData scanData = new ScanData(scanDataKey, "1985", p1Start, p1End, p2Start, p2End, otStart, otEnd);
        //            gennericDao.remove(scanData);
        //            gennericDao.create(scanData);
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }

}

From source file:graph.plotter.PieMenu.java

/**
 * This method works for choosing file path
            //  w  w w  .java2s .  co  m
    */
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    // TODO add your handling code here:
    JFileChooser jf = new JFileChooser();
    jf.showOpenDialog(null);
    File file = jf.getSelectedFile();
    String f = file.getAbsolutePath();
    jTextField3.setText(f);
}

From source file:de.atomfrede.tools.evalutation.tools.plot.ui.wizard.time.pages.TimeFileSelectionPage.java

@Override
public JButton getSelectInputFileButton() {
    if (selectInputFileButton == null) {
        selectInputFileButton = new JButton(Icons.IC_MIME_CSV_SMALL);
        selectInputFileButton.setText("Select Data File");

        selectInputFileButton.addActionListener(new ActionListener() {

            @Override//from  w w  w .  j a  va 2s . c o m
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                JFileChooser fc = getFileChooser();
                if (dataFile != null)
                    fc.setCurrentDirectory(dataFile.getParentFile());
                int returnValue = fc.showOpenDialog(parent);

                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    dataFile = fc.getSelectedFile();
                    getInputFileTextField().setText(dataFile.getName());
                    // TODO check if that file contains a time column
                    if (checkFile()) {
                        plotWizard.setDataFile(dataFile);
                    } else {
                        plotWizard.setDataFile(null);
                    }
                }
            }
        });
    }
    return selectInputFileButton;
}

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

@Override
public void performAction() {
    Scenario scenario = (Scenario) modello.getBean(Costanti.CURRENT_SCENARIO);
    MappingTask mappingTask = scenario.getMappingTask();
    JFileChooser chooser = vista.getFileChooserSalvaFolder();
    File file;//from ww w.j  a v  a2 s .co m
    int returnVal = chooser.showDialog(WindowManager.getDefault().getMainWindow(),
            NbBundle.getMessage(Costanti.class, Costanti.EXPORT));
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        try {
            file = chooser.getSelectedFile();
            DAOCsv daoCsv = new DAOCsv();
            daoCsv.exportTranslatedCSVinstances(mappingTask, file.getAbsolutePath(), scenario.getNumber());
            //giannisk canonical tree not needed
            ////daoCsv.exportCanonicalCSVinstances(mappingTask.getMappingData().getCanonicalSolution().getDataSource(), file.getAbsolutePath());
            DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
                    NbBundle.getMessage(Costanti.class, Costanti.EXPORT_COMPLETED_OK)));
            //StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(Costanti.class, Costanti.EXPORT_COMPLETED_OK));
        } catch (DAOException ex) {
            logger.error(ex);
            DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
                    NbBundle.getMessage(Costanti.class, Costanti.EXPORT_ERROR) + " : " + ex.getMessage(),
                    DialogDescriptor.ERROR_MESSAGE));
        }
    }
}

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

@Override
public void performAction() {
    Scenario scenario = (Scenario) modello.getBean(Costanti.CURRENT_SCENARIO);
    MappingTask mappingTask = scenario.getMappingTask();
    JFileChooser chooser = vista.getFileChooserSalvaFolder();
    File file;/*from   ww  w  . ja v  a 2 s  .  co m*/
    int returnVal = chooser.showDialog(WindowManager.getDefault().getMainWindow(),
            NbBundle.getMessage(Costanti.class, Costanti.EXPORT));
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        try {
            file = chooser.getSelectedFile();
            DAOXsd daoXsd = new DAOXsd();
            daoXsd.exportTranslatedXMLinstances(mappingTask.getMappingData().getSolution().getDataSource(),
                    file.getAbsolutePath());
            //giannisk canonical tree not needed
            ////daoXsd.exportCanonicalXMLinstances(mappingTask.getMappingData().getCanonicalSolution().getDataSource(), file.getAbsolutePath());
            StatusDisplayer.getDefault()
                    .setStatusText(NbBundle.getMessage(Costanti.class, Costanti.EXPORT_COMPLETED_OK));
        } catch (DAOException ex) {
            DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
                    NbBundle.getMessage(Costanti.class, Costanti.EXPORT_ERROR) + " : " + ex.getMessage(),
                    DialogDescriptor.ERROR_MESSAGE));
            logger.error(ex);
        }

    }
}