20-CS-694 Advanced Programming Techniques Spring 2012
Widgets

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

     Previous     Applet Source      All lectures     

JFileChooser Test

A simple file editor with menu selections: load, save, save as, quit.
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

class Editor extends JFrame implements ActionListener {
   JTextField messages = new JTextField();
   JTextArea text = new JTextArea();
   MenuItem load, save, saas, quit;
   String file, savefile;
   
   Editor() {
      super("Editor");
      setLayout(new BorderLayout());
      add ("North", messages);
      messages.setEditable(false);
      add ("Center", text);
      Menu menu = new Menu ("File");
      menu.add (load = new MenuItem("Load ..."));
      menu.add (save = new MenuItem("Save"));
      menu.add (saas = new MenuItem("Save As..."));
      menu.add (quit = new MenuItem("Quit"));
      MenuBar mb = new MenuBar();
      mb.add(menu);
      setMenuBar (mb);
      setSize(600,440);
      setVisible(true);
      load.addActionListener(this);
      save.addActionListener(this);
      saas.addActionListener(this);      
      quit.addActionListener(this);
      messages.setText("Editing Clean Slate");
   }
   
   public void actionPerformed (ActionEvent e) {
      if (e.getSource() == load) loadFile();
      else if (e.getSource() == saas) saveFile();
      else if (e.getSource() == save) saveIt();
      else if (e.getSource() == quit) dispose();
   }
   
   public void loadFile() {
      JFileChooser fd;
      try {
	 fd = new JFileChooser ();
	 int r;
         if ((r=fd.showOpenDialog(this)) == JFileChooser.APPROVE_OPTION)
	    savefile = fd.getSelectedFile().getName();
	 else
	    return;
      } catch (Exception e) {
         messages.setText(String.valueOf(e));
         savefile = "abcd";
      }

      try {
         FileInputStream fis = new FileInputStream (savefile);
         byte data[] = new byte[fis.available()];
         fis.read(data);
         text.setText(new String(data));
         messages.setText("Editing file "+savefile);
      }
      catch (Exception e) {
         messages.setText("Editing new file");
      }
   }
   
   public void saveFile() {
      JFileChooser fd;
      try {
	 fd = new JFileChooser ();
         if (fd.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
	    savefile = fd.getSelectedFile().getName();
	 else
	    return;
      } catch (Exception e) {
         messages.setText(String.valueOf(e));
         savefile = "abcd";
      }	 

      try {
         FileOutputStream fos = new FileOutputStream (savefile);
         String out = text.getText();
         byte data[] = new byte[out.length()];
         for (int i=0 ; i < out.length() ; i++)
            data[i] = (byte)out.charAt(i);
         fos.write(data);
         messages.setText("Contents saved to "+savefile);
      }
      catch (Exception e) {
         messages.setText("Cannot write to file - read only?");
      }
   }
   
   public void saveIt() {
      if (savefile == null) {
         messages.setText("No File To SAVE!!!  Use Save As...");
         return;
      }
      try {
         FileOutputStream fos = new FileOutputStream (savefile);
         String out = text.getText();
         byte data[] = new byte[out.length()];
         for (int i=0 ; i < out.length() ; i++)
            data[i] = (byte)out.charAt(i);
         fos.write(data);
         messages.setText("Contents saved to "+savefile);
      }
      catch (Exception e) {
         messages.setText("Cannot write to file "+savefile+" - read only?");
      }
   }
}

public class GetFiles extends Applet {
   public void init() {
      Editor e = new Editor();
   }
}