Text Editor

I am a beginning Java developer who was looking for sample Java code so that I could use to get me started creating Java apps for the Z. I was having a hard time finding any samples that I could use. I have added some sample code from a little app that I created. It retrieves the value from a file called wow2.txt, lets you edit it and then writes the value back to the file.

What you will need - JDK (latest version)  - get from http://java.sun.com

How to create, compile and run the app -

Create a new file called PersonalProfileApp. Copy the code below, paste it into the file and save the file into your c:\jdk1.3.1_03\bin folder.  (Or whatever folder that matches the JDK version you installed)

Create a new file called wow2.txt.  Enter "Text from wow2.txt" into the file and save the file into your c:\jdk1.3.1_03\bin folder.  (Or whatever folder that matches the JDK version you installed)

Next compile the code from a command line from your  c:\jdk1.3.1_03\bin folder using "javac PersonalProfileApp.java".  See the HelloWorld example for instructions for more details on how to compile an app.)

Now run the app from that same folder and command window using "java PersonalProfileApp".

You should now see the app appear.

Output -

wpe8.jpg (6476 bytes)

Code -

Here is the code for PersonalProfileApp.java

/**
* This is a simple java app that I created that will create and edit a text file called
* wow2.txt. The wow2.txt file will be created automatically the first time you hit the "Save"
* button on the app. This text file will be created in the same folder as the class file.
* This class can be run on the Zaurus from a shell command using "evm PersonalProfileApp".
* It is very simple, and is meant to be used as the building block for those starting Java
* development (including me).
*/

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class PersonalProfileApp extends Frame implements
ActionListener {

Label panelLabel = new Label("Panel");
Button button = new Button("Save");
Button button3 = new Button("Open");
Button button2 = new Button("Exit");
Panel panel = new Panel();
Panel panel2 = new Panel();
Label label = new Label("Button:");
int count = 0;
TextArea textfield = new TextArea(4,15);
public void start(String args) {
setLayout(new FlowLayout(FlowLayout.CENTER));

panel2.setLayout(new FlowLayout(FlowLayout.CENTER,2,2));
panel2.add(textfield);
add("North", panel2);

panel.setLayout(new FlowLayout(FlowLayout.CENTER,2,2));
panel.add(button);
panel.add(button2);
panel.add(button3);
add("North", panel);

button.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);

validate();


}

public void getstuff(String args[]) throws Exception{
PersonalProfileApp testApp = new PersonalProfileApp();
FileReader fr = new FileReader("wow.txt");
BufferedReader br = new BufferedReader(fr);
System.out.println(args);
String s;
while((s=br.readLine()) !=null)
{
System.out.println(s);
}

fr.close();
}



//-----------------------------------------------------------
// ActionListener Interface
//-----------------------------------------------------------

public void actionPerformed(ActionEvent event) {

String str = event.getActionCommand();

if (str == "Exit")

System.exit(0);


if (str == "Save"){

String gettext = textfield.getText();
try{
FileWriter fw = new FileWriter("wow2.txt");
fw.write(gettext);
fw.close();
}
catch(Exception e)
{
e.printStackTrace();
}

}



if (str == "Open"){

try{
FileReader fr = new FileReader("wow2.txt");
BufferedReader br = new BufferedReader(fr);
String ss;
String aa = "";
while((ss=br.readLine()) !=null)
{
aa = aa + ss;
}

textfield.setText(aa);

fr.close();
}
catch(Exception ee)
{
ee.printStackTrace();
}

}


}

public static void main(String argv[])throws Exception {

PersonalProfileApp testApp = new PersonalProfileApp();

FileReader fr = new FileReader("wow2.txt");
BufferedReader br = new BufferedReader(fr);
String s;
String a = "";
while((s=br.readLine()) !=null)
{
a = a + s;

}

fr.close();

testApp.start(a);
testApp.setSize(220, 200);
testApp.validate();
testApp.show();

}
}