Quote Saver                              Peter Komisar



/*
This code has a few pieces that are frequently looked for when building a small app. There is
a routine to save to file via the JFileChooser class.There is also an anoymous window closer
*/

/* Build a new directory called Quotes. Save your quotes
   with this tool to the name of the author of the quote */
 
import javax.swing.*;
import java.io.*;
import java.awt.*;  // for layout managers
import java.awt.event.*;

class FileHandler{
JFrame jf;
FileHandler(){
 jf= new JFrame
 ("Enter a quote and save to author's name");
 

 final JButton jb=new JButton("Press to save quote");
 final JTextArea jta=new JTextArea(20,40);
    jta.setFont(new Font("Helvitica",Font.PLAIN,14));
    jta.setBackground(new Color(240,250,255));

//using an anonymous listener to write to file

jb.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent ae){
    JFileChooser fc=new JFileChooser("Save to Authors name" );
    fc.setDialogType(JFileChooser.SAVE_DIALOG);
    fc.setDialogTitle("  Save to authors name ");
    fc.showSaveDialog(jf);
 
 try{
    File f=fc.getSelectedFile();
    FileOutputStream fos=new FileOutputStream(f);
    DataOutputStream dos=new DataOutputStream(fos);
    dos.writeBytes(jta.getText());
    }catch(FileNotFoundException fnfe){
          System.out.println("File not found");
          }
     catch(IOException ioe){
          System.out.println("IOException");
          }
     catch(Exception e){
          System.exit(0);
          }
  }
});
 // add an anonymous window closer

 jf.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent we){
     System.exit(0);
     }
   });
 
 jf.getContentPane().add(jta,BorderLayout.CENTER);
 jf.getContentPane().add(jb,BorderLayout.SOUTH);
 jf.setSize(400,200);
 jf.setLocation(200,200);
 jf.show();
 }

public static void main(String[]args){
   new FileHandler( );
   }
}