Java JFrame Show showAndSaveReportFile(JFrame frmParent, File defaultReportFile, String data)

Here you can find the source of showAndSaveReportFile(JFrame frmParent, File defaultReportFile, String data)

Description

show And Save Report File

License

Open Source License

Declaration

public static void showAndSaveReportFile(JFrame frmParent, File defaultReportFile, String data) 

Method Source Code


//package com.java2s;
import java.io.*;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

public class Main {
    public static void showAndSaveReportFile(JFrame frmParent, File defaultReportFile, String data) {
        JFileChooser fc = createReportFileChooser(defaultReportFile);
        int returnVal = fc.showSaveDialog(frmParent);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();

            try {
                FileWriter writer = new FileWriter(file);
                writer.write(data);/* w  w w.  ja va2  s .c  om*/
                writer.flush();
                writer.close();
                JOptionPane.showMessageDialog(frmParent, "Save sucessfully!");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static JFileChooser createReportFileChooser(File defaultReportFile) {
        JFileChooser fc = new JFileChooser();
        fc.setFileFilter(new FileFilter() {
            @Override
            public boolean accept(File f) {
                String filename = f.getName();
                if (filename.endsWith(".html") || filename.endsWith(".htm") || filename.endsWith(".xhtml"))
                    return true;
                return false;
            }

            @Override
            public String getDescription() {
                return "HTML file";
            }
        });
        if (defaultReportFile != null)
            fc.setSelectedFile(defaultReportFile);
        return fc;
    }
}

Related

  1. isShowing(JFrame f)
  2. open(JFrame view)
  3. show(JFrame frame)
  4. showDemoFrame(JFrame frame)
  5. showFrame(JFrame frame)
  6. showFrame(JFrame frame, JFrame parent)
  7. showHelp(JFrame frame, String path)