![]() |
Java Application Tutorial #6
To complete our small text editor, here is the rest of the code including the Save and Load which are very similar:-
import java.awt.*;
import java.io.*;
import java.util.*;
public class TestApp
{
public static void main(String args[])
{
myFrame Window;
if (args.length > 0)
{
Window=new myFrame(args[0]);
}
else
{
Window=new myFrame("Untitled");
Window.show();
Window.resize(Window.preferredSize());
Window.pack();
}
}
}
class myFrame extends Frame
{
private Dimension myDimension;
TextArea myTextArea=new TextArea(" ",25,80);
String gblFilename;
String gblDirectory;
myFrame(String Title)
{
super(Title);
setLayout(new GridLayout(1,1));
this.add(myTextArea);
myDimension = new Dimension(200,200);
myTextArea.appendText("This is copyright\n2004");
// menu's
MenuBar mb = new MenuBar();
Menu m = new Menu("File");
m.add(new MenuItem("Load"));
m.add(new MenuItem("Save"));
m.add(new MenuItem("Exit"));
mb.add(m);
setMenuBar(mb);
}
public Dimension minimumSize()
{
return myDimension;
}
public Dimension preferredSize()
{
return myDimension;
}
public boolean handleEvent(Event ev)
{
if (ev.id == Event.WINDOW_DESTROY)
{
this.dispose();
System.exit(0);
return true;
}
if (ev.id == Event.ACTION_EVENT)
{
if (ev.target instanceof MenuItem)
{
if (ev.arg.equals("Exit"))
{
System.exit(0);
}
if (ev.arg.equals("Load"))
{
FileDialog fd = new FileDialog(this, "Open a File");
fd.show();
if (fd.getFile() != null)
{
String filename=new String(fd.getDirectory() + fd.getFile());
gblDirectory=fd.getDirectory();
gblFilename=fd.getFile();
try
{
clsLoadFile view = new clsLoadFile(filename, myTextArea);
}
catch (IOException e)
{
System.err.println("IOException in Load menu");
}
setTitle(filename);
}
}
}
if (ev.arg.equals("Save"))
{
FileDialog fd = new FileDialog(this, "Save this File");
fd.setDirectory(gblDirectory);
fd.setFile("untitled.txt");
fd.show();
if (fd.getFile() != null)
{
String filename=new String(fd.getDirectory() + fd.getFile());
gblDirectory=fd.getDirectory();
gblFilename=fd.getFile();
try
{
clsSaveFile save = new clsSaveFile(filename, myTextArea);
}
catch (IOException e)
{
System.err.println("IOException in Save menu");
}
setTitle(filename);
}
}
}
return super.handleEvent(ev);
}
}
class clsLoadFile
{
clsLoadFile(String FileName, TextArea myTextArea) throws IOException
{
FileInputStream fis;
StringBuffer sb = new StringBuffer();
try
{
fis = new FileInputStream(FileName);
int onechar;
while ((onechar=fis.read()) != -1)
sb.append((char)onechar);
fis.close();
myTextArea.setText(sb.toString());
}
catch (IOException e)
{
System.err.println ("IOException in clsLoadFile");
}
}
}
class clsSaveFile
{
clsSaveFile(String FileName, TextArea myTextArea) throws IOException
{
FileOutputStream fos;
StringBuffer sb = new StringBuffer();
sb.append(myTextArea.getText());
int i;
try
{
fos = new FileOutputStream(FileName);
for (i=0; i < sb.length(); i++)
fos.write(sb.charAt(i));
fos.close();
}
catch (IOException e)
{
System.err.println ("IOException in clsSaveFile");
}
}
}
I've included all the code including the main test class. That completes this short tutorial. I hope it has awakened your interest in Java. If you have found it too difficult then I suggest you try one of my other BASIC programming language tutorials which you may find a little easier. I hope to do some more tutorials in the future for the new version of Java (1.2) which contains a whole host of new and exciting technologies that make programs look much more exciting and has a range of built-in library functions (such as alert boxes) that make Java much easier and more rewarding to use. Until then...