Showing posts with label simple notepad. Show all posts
Showing posts with label simple notepad. Show all posts

Saturday, 31 August 2013

Simple Notepad editor where user can open files, create and save files in text format.


/**
 * @(#)Open_Save_JMenuBar.java
 *
 *
 * @author -> Anant Mahale
 * @version 1.00 2013/8/27
 *
 *write a java program to implement a simple Notepad editor
 * where user can open files,
 * create and save files in text format
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class Open_Save_JMenuBar
{
    public static void main(String []args)
    {
        myframe m=new myframe();
        m.setTitle("Simple JMenu");
        m.setSize(500,500);
        m.setVisible(true);
        m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class myframe extends JFrame
{
    mypanel p;
    myframe()
    {
        p=new mypanel();
        Container c=getContentPane();
        c.add(p);
    }
    class mypanel extends JPanel implements ActionListener
    {
        JMenu file;        
        JMenuItem new1,open,save;                                     
        JTextArea txta_show;   
        String msg="";
        JMenuBar mb=new JMenuBar();
        mypanel()
        {
            setLayout(new BorderLayout());
            file=new JMenu("File");
           
            new1=new JMenuItem("New");
            new1.addActionListener(this);
           
            open=new JMenuItem("Open");
            open.addActionListener(this);
           
            save=new JMenuItem("Save");
            save.addActionListener(this);
           
            file.add(new1);
            file.add(open);
            file.add(save);
           
            mb.add(file);
            setJMenuBar(mb);
           
            txta_show=new JTextArea();
            JScrollPane pane=new JScrollPane(txta_show);
            add(pane);           
        }
        public void actionPerformed(ActionEvent ae)
        {   
            String str="";
            String temp="";
            if(ae.getSource()==new1)
            {
                txta_show.setText("");
            }
            if(ae.getSource()==open)
            {
                try
                {
                    JFileChooser jfc1=new JFileChooser();
                    jfc1.showOpenDialog(this);
                    File fname1=jfc1.getSelectedFile();
                    FileReader fr=new FileReader(fname1.getPath());
                    BufferedReader bf=new BufferedReader(fr);
                    while((str=bf.readLine())!=null)
                    {
                        temp=temp+str+"\n";
                    }
                    fr.close();
                    txta_show.setText(temp);
                    setTitle(fname1.getPath());
                   
                }
                catch(Exception e)
                {
                    System.out.println(e);
                }
            }   
            if(ae.getSource()==save)
            {
                String str_gettext=txta_show.getText();       
                JFileChooser chooser = new JFileChooser();
                chooser.setCurrentDirectory( new File( "./") );
                int actionDialog = chooser.showSaveDialog(this);
                if (actionDialog == JFileChooser.APPROVE_OPTION)
                {
                    File fileName = new File(chooser.getSelectedFile( ) + "" );
                    if(fileName == null)
                        return;
                    if(fileName.exists())
                    {
                        actionDialog = JOptionPane.showConfirmDialog(this,"Replace existing file?");
                        if (actionDialog == JOptionPane.NO_OPTION)
                            return;
                    }
                    try
                    {
                        BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
                            out.write(str_gettext);
                            out.close();
                    }
                    catch(Exception e)
                    {
                         System.err.println("Error: " + e.getMessage());
                    }
                }
            }
        }
    }
}

Friday, 14 June 2013

program in java - all menu in simple notepad with awt


/**
 * @(#)Text1.java
 *
 *
 * @author
 * @version 1.00 2013/4/26
 */
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyMenues extends Frame implements ActionListener
{
    private Menu file,Edit,format,View,Help;
    private MenuBar mb=new MenuBar();
    private MenuItem New,Open,Save,Save_as,page_setup,print,exit,undo,redo,cut,copy,paste,delete,find,find_next,replace,goto_,time_date,fontwrap,font_,statusbar,view_help,about_notpad;                                         
    private TextArea txta_show;   
    private String msg="";
    public MyMenues()
    {
       
        setTitle("Notepad");
        setSize(500,500);
        setLocation(100,100);       
        /*----------------file----------------*/
        New=new MenuItem("New");
       
        Open=new MenuItem("Open");
        Save=new MenuItem("Save");
        Save_as=new MenuItem("Save As");
        page_setup=new MenuItem("Page Setup");
        print=new MenuItem("Print");
        exit=new MenuItem("Exit");
       
        file=new Menu("File");
   
        file.add(New);
        file.add(Open);
        file.add(Save);
        file.add(Save_as);
        file.addSeparator();
        file.add(page_setup);
        file.add(print);
        file.addSeparator();
        file.add(exit);
       
        mb.add(file);       
        /*-------------Edit--------------*/
        undo=new MenuItem("Undo");
        redo=new MenuItem("Redo");
        cut=new MenuItem("Cut");
        copy=new MenuItem("Copy");
        paste=new MenuItem("Paste");
        delete=new MenuItem("Delete");
        find=new MenuItem("Find");
        find_next=new MenuItem("Find Next");
        replace=new MenuItem("Replace");
        goto_=new MenuItem("Goto");
        time_date=new MenuItem("Time/Date");
       
        Edit=new Menu("Edit");   
               
        Edit.add(undo);
        Edit.add(redo);
        Edit.addSeparator();
        Edit.add(cut);
        Edit.add(copy);
        Edit.add(paste);
        Edit.add(delete);
        Edit.addSeparator();
        Edit.add(find);
        Edit.add(replace);
        Edit.add(goto_);
        Edit.addSeparator();
        Edit.add(time_date);
       
        mb.add(Edit);       
        /*--------------format------------------*/       
        fontwrap=new MenuItem("Font Wrap");
        font_=new MenuItem("Font");
               
        format=new Menu("Format");
       
        format.add(fontwrap);
        format.add(font_);
       
        mb.add(format);       
        /*------------------view-----------------*/       
        statusbar=new MenuItem("StatusBar");
       
        View=new Menu("View");
       
        View.add(statusbar);
       
        mb.add(View);
               
        /*-----------------help-----------------*/       
        view_help=new MenuItem("View Help");
        about_notpad=new MenuItem("About Notepad");
       
        Help=new Menu("Help");
       
        Help.add(view_help);
        Help.addSeparator();
        Help.add(about_notpad);
       
        mb.add(Help);       
        /*-------------------textarea--------------------*/       
        txta_show=new TextArea();
        add(txta_show);
       
        setMenuBar(mb);
        /*-------------------------------------*/
       
       
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
               
            }
        });
        /*---------------addactionlistener------------------*/
        New.addActionListener(this);
        Open.addActionListener(this);
        Save.addActionListener(this);
        Save_as.addActionListener(this);
        page_setup.addActionListener(this);
        print.addActionListener(this);
        exit.addActionListener(this);
        undo.addActionListener(this);
        redo.addActionListener(this);
        cut.addActionListener(this);
        copy.addActionListener(this);
        paste.addActionListener(this);
        delete.addActionListener(this);
        find.addActionListener(this);
        find_next.addActionListener(this);
        replace.addActionListener(this);
        goto_.addActionListener(this);
        time_date.addActionListener(this);
        fontwrap.addActionListener(this);
        font_.addActionListener(this);
        statusbar.addActionListener(this);
        view_help.addActionListener(this);
        about_notpad.addActionListener(this);                                            
    }
    public void actionPerformed(ActionEvent ae)
    {
        /*---------------file menu----------------------*/
        if(ae.getSource()==New)
        {
            txta_show.setText(" ");
        }
        else if(ae.getSource()==Open)
        {
            try
            {
                FileDialog fd=new FileDialog(this,"Open File",FileDialog.LOAD);
                fd.setVisible(true);
                String dir=fd.getDirectory();
                String fname=fd.getFile();
                FileInputStream fis=new FileInputStream(dir+fname);
                DataInputStream dis=new DataInputStream(fis);
                String str=" ",msg=" ";
                while((str=dis.readLine())!=null)
                {
                    msg=msg+str;
                    msg+="\n";
                }
                txta_show.setText(msg);
                dis.close();
            }
            catch(Exception e){}
        }
        else if(ae.getSource()==Save)
        {
            try
            {
                FileDialog fd=new FileDialog(this,"Save File",FileDialog.SAVE);
                fd.setVisible(true);
                String txt=txta_show.getText();
                String dir=fd.getDirectory();
                String fname=fd.getFile();
                FileOutputStream fos=new FileOutputStream(dir+fname);
                DataOutputStream dos=new DataOutputStream(fos);
                dos.writeBytes(txt);
                dos.close();
            }
            catch(Exception e)
            {
            }
        }
        else if(ae.getSource()==exit)
        {
            new Exit_Window().setVisible(true);
        }
        /*------------------Edit Menu---------------------------*/
        else if(ae.getSource()==cut)
        {
            msg=txta_show.getSelectedText();
            txta_show.replaceRange("",txta_show.getSelectionStart(),txta_show.getSelectionEnd());
        }
        else if(ae.getSource()==copy)
        {
            msg=txta_show.getSelectedText();
        }
        else if(ae.getSource()==paste)
        {
            txta_show.replaceRange("",txta_show.getSelectionStart(),txta_show.getSelectionEnd());
        }
        else if(ae.getSource()==replace)
        {
        //    new replaceBox().setVisible(true);
        }
        /*-----------------------Help Menu------------------------------*/
        else if(ae.getSource()==about_notpad)
        {
            new MyDialog().setVisible(true);   
        }       
    }
}
class Exit_Window extends Dialog implements ActionListener
{
    private Label lbl_show_msg;
    private Button btn_yes,btn_no;
    public Exit_Window()
    {
        super(new MyMenues(),"Exit",true);
        setLayout(new FlowLayout());       
       
        lbl_show_msg=new Label("Are you want to exit ??");
        add(lbl_show_msg);
       
        btn_yes=new Button("Yes");
        btn_yes.addActionListener(this);
        add(btn_yes);
       
        btn_no=new Button("No");
        btn_no.addActionListener(this);
        add(btn_no);
       
        setTitle("Exit Window");
        setSize(150,100);
        setLocation(200,200);
    }
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==btn_yes)
        {
            System.exit(0);
        }
        else
            dispose();   
    }
}
/*
class replaceBox extends Dialog implements ActionListener
{
    private Label lbl_replace,lbl_replacewith;
    private TextField txt_replace,txt_replacewith;
    private Button btn_change,btn_close;
    public replaceBox()
    {
        setLayout(null);
       
        lbl_replace=new Label("Replace");
        lbl_replace.setBounds(50,50,100,30);
        add(lbl_replace);
       
        txt_replace=new TextField();
        txt_replace.setBounds(100,50,100,30);
        add(txt_replace);
       
        lbl_replacewith=new Label("ReplaceWith");
        lbl_replacewith.setBounds(50,100,100,30);
        add(lbl_replacewith);
       
        txt_replacewith=new TextField();
        txt_replacewith.setBounds(100,100,100,30);
        add(txt_replacewith);
       
        btn_change=new Button("Change");
        btn_change.setBounds(50,150,50,30);
        add(btn_change);
       
        btn_close=new Button("Close");
        btn_close.setBounds(75,150,125,30);
        add(btn_close);
       
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
               
            }
        });
    }
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==btn_change)
        {
               
        }
    }
}
*/
class MyDialog extends  Dialog implements  ActionListener
{
    private Label lbl_msg;
    private Button btn_yes,btn_no;
    private Panel north_panel,south_panel;
    public MyDialog()
    {
        super(new MyMenues(),"Dialog Demo",true);
        north_panel=new Panel();
        south_panel=new Panel();
       
        setTitle("Notepad Information");
        setSize(400,200);
        setLocation(50,80);
        lbl_msg=new Label("This notepad made my Anant Mahale");
        btn_yes=new Button("Yes");
       
        north_panel.add(lbl_msg);
        south_panel.add(btn_yes);
        add(south_panel,"South");
        add(north_panel,"North");
        btn_yes.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==btn_yes)
            dispose();
    }
}
class FullMenu
{
    public static void main(String []args)
    {
        new MyMenues().setVisible(true);
    }
}