Saturday, 10 August 2013

how to create notepad using java?

Simple java program
.
we need three program

first main program called NOTEPAD:
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Notepad extends Frame implements ActionListener
{
MenuBar mb;
Menu file,edit;
MenuItem ne,open,save,quit,copy,cut,paste,find,replace;
FileDialog fd;
String sel;
TextArea ta;
MyDialog p;
public Notepad()
{
setTitle("untitled");
setSize(500,500);
setLayout(new BorderLayout());
mb=new MenuBar();
file=new Menu("File");
ne=new MenuItem("new");
ne.addActionListener(this);
open=new MenuItem("open");
open.addActionListener(this);
save=new MenuItem("save");
save.addActionListener(this);
quit=new MenuItem("quit");
quit.addActionListener(this);
file.add(ne);
file.add(open);
file.add(save);
file.add(quit);
edit=new Menu("Edit");
cut=new MenuItem("cut");
cut.addActionListener(this);
copy=new MenuItem("copy");
copy.addActionListener(this);
paste=new MenuItem("paste");
paste.addActionListener(this);
find=new MenuItem("find");
find.addActionListener(this);
replace=new MenuItem("replace");
replace.addActionListener(this);
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(find);
edit.add(replace);
mb.add(file);
mb.add(edit);
setMenuBar(mb);
ta=new TextArea(300,300);
add (ta,BorderLayout.CENTER);
addWindowListener(new myWindowAdapter(this));
}
public static void main(String args[])
{
Notepad np=new Notepad();
np.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
   if(ae.getSource()==ne)
  {
   this.setTitle("untitled");
   ta.setText("");
   ta.requestFocus();
}
else if(ae.getSource()==open)
{
fd=new FileDialog(this,"open",FileDialog.LOAD);
fd.setVisible(true);
String filename=fd.getFile();
String dir=fd.getDirectory();
try
{
FileInputStream fin=new FileInputStream(dir+filename);
int x;
while((x=fin.read())!=-1)
{
ta.append(""+(char)x);
}
fin.close();
}
catch(FileNotFoundException e)
{
System.out.println("file doesnt exist");
}
catch(IOException e)
{
System.out.println("IO Exception");
}
}
else if(ae.getSource()==save)
{
fd=new FileDialog(this,"save As",FileDialog.SAVE);
fd.setVisible(true);
String filename=fd.getFile();
String dir=fd.getDirectory();
try
{
FileOutputStream fout=new FileOutputStream(dir+filename);
String text=ta.getText();
fout.write(text.getBytes());
fout.close();
}
catch(FileNotFoundException e)
{
System.out.println("file cannot be saved");
}
catch(IOException e)
{
System.out.println("IO Exception");
}
}
else if(ae.getSource()==quit)
{
this.setVisible(false);
p.dispose();
System.exit(0);
}

else if(ae.getSource()==cut)
{
sel =ta.getSelectedText();
int start=ta.getSelectionStart();
String text=ta.getText();
String prefix=text.substring(0,start);
String suffix=text.substring(start+sel.length());
ta.setText(prefix+suffix);
}
else if(ae.getSource()==copy)
{
sel=ta.getSelectedText();
}
else if(ae.getSource()==paste)
{

int start=ta.getSelectionStart();
String text=ta.getText();
String prefix=text.substring(0,start);
String suffix=text.substring(start);
ta.setText(prefix+suffix+sel);
}
else if(ae.getSource()==find)
{
if(p==null||!(p.getTitle().equals("Find")))
p=new MyDialog(this,"Find");
else p.setVisible(true);
}
else if(ae.getSource()==replace)
{
if(p==null||!(p.getTitle().equals("Replace")))
p=new MyDialog(this,"Replace");
else
 p.setVisible(true);
}
}}




second program for WINDOWS ADAPTER:


import java.awt.event.*;
public class myWindowAdapter extends WindowAdapter
{
Notepad np;
public myWindowAdapter(Notepad n)
{
np=n;
}
public void windowClosing(WindowEvent we)
{
np.setVisible(false);
if(np.p!=null)
np.p.dispose();
System.exit(0);
}
}


third program for DAILOG BOX


import java.awt.event.*;
import java.awt.*;
public class MyDialog extends Dialog implements ActionListener
{
TextField  findText,replaceText;
Notepad np;
Button find,findnext,replace,replaceall,close;
Panel p1,p2;
String ft;
String rw;
int st=0;
public MyDialog(Notepad n,String title)
{
super(n,title,false);
setLayout(new BorderLayout());
np=n;
p1=new Panel();
p2=new Panel();
findText=new TextField(20);
replaceText=new TextField(20);
p1.add(new Label ("Find what"));
p1.add(findText);
find=new Button("find");
find.addActionListener(this);
p2.add(find);
findnext=new Button("findnext");
findnext.addActionListener(this);
p2.add(findnext);
if(title.equals("Replace"))
{
p1.add(new Label("replace with"));
p1.add(replaceText);
replace=new Button("replace");
replace.addActionListener(this);
p2.add(replace);
replaceall=new Button("replaceall");
replaceall.addActionListener(this);
p2.add(replaceall);
}
close=new Button("close"); 
close.addActionListener(this);
p2.add(close);
add(p1,BorderLayout.CENTER);
add(p2,BorderLayout.SOUTH);
setSize(350,150);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
setVisible(false);
}
});
}
public void actionPerformed(ActionEvent ae)
{
     if(ae.getSource()==find)
     {
        ft=findText.getText();
         String text=np.ta.getText();
        st=text.indexOf(ft);
        if(st!=-1)
         {
              np.ta.setSelectionStart(st);
               np.ta.setSelectionEnd(st+ft.length());
                np.ta.requestFocus();
          }
        else
              {
                    final Dialog d=new Dialog(this,"Notepad",true);
                   d.add(new Label("string not found"),BorderLayout.CENTER);
                   Panel p=new Panel();
                   Button ok=new Button("ok");
                    p.add(ok);
                     ok.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent ae)
          {
          d.dispose();
          }
          });
        d.addWindowListener(new WindowAdapter()
        {
        public void windowClosing(WindowEvent we)
        {
        d.dispose();
        }
       });
                d.add(p,BorderLayout.SOUTH);
                 d.setSize(175,100);
                 d.setVisible(true);
            }
    }
          else if(ae.getSource()==findnext)
           {
                  ft=findText.getText();
                     String text=np.ta.getText();
                     st=text.indexOf(ft,st+ft.length());
                       if(st!=-1)
                        {
                           np.ta.setSelectionStart(st);
                          np.ta.setSelectionEnd(st+ft.length());
                          np.ta.requestFocus();
                        }
               else
                 {
                      final Dialog d=new Dialog(this,"Notepad",true);
                      d.add(new Label("string not found"),BorderLayout.CENTER);
                       Panel p=new Panel();
                           Button ok=new Button("ok");
                           p.add(ok);
                                      ok.addActionListener(new ActionListener(){
                                       public void actionPerformed(ActionEvent ae)
                                      {
                                       d.dispose();
                                       }
                                       });
                                        d.addWindowListener(new WindowAdapter()
                                      {
                                              public void windowClosing(WindowEvent we)
                                      {
                                      d.dispose();
                                      }
                                     });
                                     d.add(p,BorderLayout.SOUTH);
                                    d.setSize(175,100);
                                      d.setVisible(true);
                         }

             else if(ae.getSource()==replaceall)
             {
                         ft=findText.getText();
                       rw=replaceText.getText();
                         st=0;
                      int count=0;
                  while(st!=-1)
                      {
                         String text=np.ta.getText();
                          st=text.indexOf(ft);
                     if(st!=-1)
                          {
                            np.ta.replaceRange(rw,st,st+ft.length());
                               np.ta.requestFocus();
                              count++;
                           }
                else
                   {
                      final Dialog d=new Dialog(this,"Notepad",true);
                         d.add(new Label("string not found"),BorderLayout.CENTER);
                          Panel p=new Panel();
                        Button ok=new Button("ok");
                      p.add(ok);
                  ok.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent ae)
                         {
                            d.dispose();
                         }
                     });
                        d.addWindowListener(new WindowAdapter()
                           {
                                public void windowClosing(WindowEvent we)
                             {
                       d.dispose();
                       }
                       });
                        d.add(p,BorderLayout.SOUTH);
                          d.setSize(175,100);
                            d.setVisible(true);
                    }
              }
         }
}
}




COMPILE:
save all the three file in same drive

sample output


No comments:

Post a Comment