Showing posts with label java swing notepad. Show all posts
Showing posts with label java swing 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());
                    }
                }
            }
        }
    }
}