Simpe Text Editor using java GUI

Hello friends.
       Here is a simple TextEditor using basic java gui.



import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class UI extends JFrame
{
    File mainFile;
     JMenu jMenu1= new javax.swing.JMenu();
    UI()
    {
        setUndecorated(true);
        setTitle("Text Editor ");
        setSize(1200,700);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        Toolkit toolkit=getToolkit();
        Dimension size=toolkit.getScreenSize();
        setLocation(size.width/2-getWidth()/2,size.height/2-getHeight()/2);

        JButton save=new JButton("Save");
        add(save);
       
        JButton open=new JButton("Open");
        add(open);

        JButton newFile=new JButton("New File");
        add(newFile);
       
        JScrollPane jScrollPane1=new JScrollPane();

        final JTextArea input=new JTextArea(40,100);
        jScrollPane1.setViewportView(input);
        add(jScrollPane1);
        setVisible(true);
        open.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ev)
            {
                 JFileChooser jfc=new JFileChooser();
               
               
                if(jfc.showOpenDialog(jMenu1)==JFileChooser.APPROVE_OPTION)
                {
                    try
                        {
                           mainFile= jfc.getSelectedFile();
                           FileReader fr=new FileReader(mainFile);
                           BufferedReader br=new BufferedReader(fr);
                           String x;
                           String str="";
                           while((x=br.readLine())!=null)
                           {
                               str+=x+"\n";
                           }
                           input.setText(str);
                           fr.close();
                           br.close();
                        }
                    catch(FileNotFoundException e)
                    {
                        JOptionPane.showMessageDialog(null,"File not found !");
                    }
                    catch(Exception e)
                    {

                    }
           
        }
            }
        });
        save.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ev)
            {
                if(mainFile==null)
                {
                    JOptionPane.showMessageDialog(null,"Please Select the File First !");
                    return ;
                }

                try{
                    FileWriter fr=new FileWriter(mainFile);
                    BufferedWriter bw=new BufferedWriter(fr);
                    String x=input.getText();
                    fr.write(x);
                    bw.close();
                    fr.close();
                    JOptionPane.showMessageDialog(null,"File Saved !");
                }
                catch(Exception e){

                }
            }

        });
        newFile.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                //String newFileName=JOptionPane.showInputDialog("Enter File Name : ");
                //JOptionPane.showMessageDialog(null,newFileName);
                JFileChooser jfc=new JFileChooser();
                if(jfc.showOpenDialog(jMenu1)==JFileChooser.APPROVE_OPTION)
                {
                    mainFile=jfc.getSelectedFile();
                    JOptionPane.showMessageDialog(null,mainFile);
                    input.setText("");
                }
            }
        });

    }
    public static void main(String[] args) {
        UI ui=new UI();
    }
}


and this is the picture of output.











Comments