Saturday, August 4, 2012

Notepad (Editor) in Java

Simple Editor by JAVA AWT

/** 
 * MakeCodeEasy :- Rahul Jain
 */


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


public class Editor implements ActionListener
{
   Frame f;
   MenuBar mb;
   Menu m1,m2,m3;
   Label l1,l2;
   TextField t1,t2;
   Button b1,b2,b3,b4;
   MenuItem nw,op,sv,svs,ext,fnd,fndr;
   TextArea ta;
   FileDialog fd;
   String name,path,n;
   int count=0;
    Frame f2;
    boolean flag;
   
    public Editor() 
    {
    f=new Frame("Editor");
    f.setSize(500,500);
   
    WindowCloser wc=new WindowCloser();
       f.addWindowListener(wc);
   
    mb=new MenuBar();
    m1=new Menu("File");
    m2=new Menu("Tools");
    ta=new TextArea();
    f.add(ta);
   
    nw=new MenuItem("New");
    op=new MenuItem("Open");
    sv=new MenuItem("Save");
    svs=new MenuItem("Save As");
    ext=new MenuItem("Exit");
    fnd=new MenuItem("Find");
    fndr=new MenuItem("Find & Replace");
   
    
    fd=new FileDialog(f,"Save As",FileDialog.SAVE);
   
    nw.addActionListener(this);
    op.addActionListener(this);
    sv.addActionListener(this);
    svs.addActionListener(this);
    ext.addActionListener(this);
    fnd.addActionListener(this);
    fndr.addActionListener(this);
    
    m1.add(nw); 
    m1.add(op); 
    m1.add(sv);
    m1.add(svs);
    m1.addSeparator(); 
    m1.add(ext); 
   
    m2.add(fnd);
    m2.add(fndr);
   
    GridLayout gl=new GridLayout(3,0);
    f2=new Frame("Find");
    f2.setSize(300,200);
    f2.setLayout(gl);
   
    Label l1=new Label("Find");
    TextField tf=new TextField(10);
    Panel p=new Panel();
    Button b1=new Button("Find Next");
    Button b2=new Button("Close");
    b2.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae1)
    {
    f2.setVisible(false);
    f2.dispose();
    }
    });
   
   
    p.add(b1);
    p.add(b2);
    f2.add(l1);
    f2.add(tf);
    f2.add(p);
   
   
    
    mb.add(m1);
    mb.add(m2);
    f.setMenuBar(mb);
    f.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e)
    {
    try
    {
    //String str=e.getActionCommand();
    MenuItem mi=(MenuItem)e.getSource();
   
    if(mi==nw)
    {
    ta.setText("");
    sv.setEnabled(true);
    }
   
   
    else if(mi==op)
    {
     fd=new FileDialog(f,"Open",FileDialog.LOAD);
     fd.setVisible(true);
     name=fd.getFile();
     path=fd.getDirectory();
     n=path+name+"";
         File fl=new File(path,name);
           FileInputStream fis=new FileInputStream(fl);
           int ch;
           ta.setText("");
           while((ch=fis.read())!=-1)
            {
               ta.appendText((char)ch+"");
            }
           sv.setEnabled(true);
           fis.close();
           f.setTitle(fd.getFile()+"-Notepad");
           
      }
         
       
      
     
    else if(mi==sv) 
    {
    fd=new FileDialog(f,"Save",FileDialog.SAVE);
    if(flag==false)
    {
         fd.setVisible(true);
         name=fd.getFile();
         path=fd.getDirectory();
         String strr=ta.getText();
         File f=new File(path,name);
     //f.createNewFile();
     String str1=ta.getText();
     FileOutputStream fos=new FileOutputStream(f);
       char arr[];
     arr=strr.toCharArray();
     for(int i=0;i<arr.length;i++)
     { 
      fos.write(arr[i]);
     }
       fos.close();
    }
       else
       {
        System.out.println(n);
         /* name=fd.getFile();
          path=fd.getDirectory();
          File f=new File(path,name);*/
          String strr1=ta.getText();
          File f=new File(n);
          FileOutputStream fos=new FileOutputStream(f);
          char arr[];
      arr=strr1.toCharArray();
      for(int i=0;i<arr.length;i++)
      { 
      fos.write(arr[i]);
      }
       fos.close();
       }  
   
   
    }
    else if(mi==svs)
    {
    fd=new FileDialog(f,"Save As",FileDialog.SAVE);
    fd.setVisible(true);
    name=fd.getFile();
    path=fd.getDirectory();
    File f=new File(path,name);
    String str1=ta.getText();
    FileOutputStream fos=new FileOutputStream(f);
    char arr[];
    arr=str1.toCharArray();
    for(int i=0;i<arr.length;i++)
   
      fos.write(arr[i]);
    }
      fos.close();
    
   
   
    }
    else if(mi==ext)
    {
       f.setVisible(false);
           f.dispose();           //release the memory
           System.exit(1);
    }
    else if(mi==fnd)
    {
       f2.setVisible(true);
   
   
   
   
    }
    else if(mi==fndr)
    {
   
    }
    }
    catch(Exception ex)
    {
   
    }
         
    }
   
   public void textValueChanged(TextEvent te)
         {
        sv.setEnabled(true);
        flag=true;
         }
    
    public static void main(String args[])
    {
    Editor ed=new Editor();
    }
}


Compile the Program :- javac Editor.java
Run the Program:- java Editor


Output:- 


Note :- In Current program close button will not work.
            For working of close button see our next post
            Window closing in Java Awt

            

14 comments:

  1. It's really helpful....

    ReplyDelete
  2. where you are getting this error "Invalid flag error"

    ReplyDelete
  3. After compiling this code ...I got deprecated interface warning.. When I compiled. It ....javac Editor.java-Xlint:deprecation....I got error detail. ...of invalid flag

    ReplyDelete
  4. Pls check this code...n also add the features of find n find replace using regex package

    ReplyDelete
  5. I think, you have to compile this code before Editor.java


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


    public class WindowCloser extends WindowAdapter
    {
    public void windowClosing(WindowEvent e)
    {
    Window w=e.getWindow();
    w.setVisible(false);
    w.dispose(); //release the memory
    System.exit(1);
    }
    }

    ReplyDelete
  6. I have already made .class file of window closing ....so I think its ok

    ReplyDelete
  7. When I am trying these command by command line, getting no error. Only some warning while compile Editor.java

    D:\notepad>javac WindowCloser.java

    D:\notepad>javac Editor.java
    Note: Editor.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.

    D:\notepad>java Editor

    ReplyDelete
  8. After running this program u will find ...2 errors ...null pointer exception ....n other one ........ .compile code .. With ...javac Editor.java-Xlint:deprecation....u will got detail warning....that is invalid flag dear......juss do it again

    ReplyDelete
  9. You are getting this warning because this code is using some deprecated methods which were aviable in earlier versions of java bt they are removed in new versions so it is not an problem the code will run succesfully

    ReplyDelete

ShareThis