Java App | Simple Text Editor

This is a simple text editor application.

Summary:

To create a simple text editor in Java, we have used a JFrame with a JPanel which contains a MenuBar and a TextArea. Then we have added Menus to the MenuBar and MenuItems to the Menus. In Format Menu, we have used 3 sub-Menus. All the MenuItems have ActionListeners to detect any action. We have used FileWriter and FileReader to write and read data in text (.txt) files.

The Menu Bar contains 4 menus:

File : New – creates new file, Open – opens existing file, Save As – saves the file, Exit – closes the application.

Edit : Cut – cuts selected text, Copy – copies selected text, Paste – pastes text from clipboard, Delete – deletes selected text, Select All –selects the entire text, Time/Date – displays time and date.

Format : Font – submenu (Courier, Helvetica, Monospaced, Serif, SansSerif, TimesRoman), Font Style – submenu (Regular, Bold, Italic, Bold Italic), Size – submenu (8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72).

About : About TextEditor – displays information about the application.

Methods used:

METHODEXPLANATION
TextEditor (String)constructor to create the text editor
ActionPerformed (ActionEvent)action listeners for all menu items
main()main method (executable)

Program:

//TextEditor - VoidCode
//Website - https://void.code.blog/
import java.awt.*;
import java.awt.print.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;     
import java.util.*;
public class TextEditor extends JFrame implements ActionListener
{
    MenuBar mbar;
    Menu file,edit,format,about,font,font1,font2;
    MenuItem item1,item2,item3,item4;
    MenuItem item5,item6,item7,item8,item9,item10;
    MenuItem item11;
    MenuItem fname1,fname2,fname3,fname4,fname5,fname6;
    MenuItem fstyle1,fstyle2,fstyle3,fstyle4;
    MenuItem fsize1,fsize2,fsize3,fsize4,fsize5,fsize6,fsize7,fsize8,fsize9,fsize10,fsize11,fsize12,fsize13,fsize14;
    JPanel mainpanel;
    TextArea text;
    Font f;
    String months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
    GregorianCalendar gcalendar;
    String command=" ";
    String str=" ",str1=" ",str2=" ",str3=" ",str4=" ",str6=" ",str7=" ",str8=" ",str9=" ",str10=" ";
    int len1,i=0,pos1,len;
    public TextEditor(String str)
    {
        super(str);
        mainpanel=new JPanel();
        mainpanel=(JPanel)getContentPane();
        mainpanel.setLayout(new FlowLayout());
        mbar=new MenuBar();
        setMenuBar(mbar);
        file=new Menu("File");
        edit=new Menu("Edit");
        format=new Menu("Format");
        about=new Menu("About");
        font=new Menu("Font");
        font1=new Menu("Font Style");
        font2=new Menu("Size");
        file.add(item1=new MenuItem("New..."));
        file.add(item2=new MenuItem("Open..."));
        file.add(item3=new MenuItem("Save As..."));
        file.add(item4=new MenuItem("Exit"));
        mbar.add(file);
        edit.add(item5=new MenuItem("Cut (Ctrl+X)"));
        edit.add(item6=new MenuItem("Copy (Ctrl+C)"));
        edit.add(item7=new MenuItem("Paste (Ctrl+V)"));
        edit.add(item8=new MenuItem("Delete"));
        edit.add(item9=new MenuItem("Select All (Ctrl+A)"));
        edit.add(item10=new MenuItem("Time/Date"));
        mbar.add(edit);
        format.add(font);
        format.add(font1);
        format.add(font2);
        font.add(fname1=new MenuItem("Courier"));
        font.add(fname2=new MenuItem("Helvetica"));
        font.add(fname3=new MenuItem("Monospaced"));
        font.add(fname4=new MenuItem("Serif"));
        font.add(fname5=new MenuItem("SansSerif"));
        font.add(fname6=new MenuItem("TimesRoman"));
        font1.add(fstyle1=new MenuItem("Regular"));
        font1.add(fstyle2=new MenuItem("Bold"));
        font1.add(fstyle3=new MenuItem("Italic"));
        font1.add(fstyle4=new MenuItem("Bold Italic"));
        font2.add(fsize1=new MenuItem("8"));
        font2.add(fsize2=new MenuItem("10"));
        font2.add(fsize3=new MenuItem("12"));
        font2.add(fsize4=new MenuItem("14"));
        font2.add(fsize5=new MenuItem("16"));
        font2.add(fsize6=new MenuItem("18"));
        font2.add(fsize7=new MenuItem("20"));
        font2.add(fsize8=new MenuItem("22"));
        font2.add(fsize9=new MenuItem("24"));
        font2.add(fsize10=new MenuItem("26"));
        font2.add(fsize11=new MenuItem("28"));
        font2.add(fsize12=new MenuItem("36"));
        font2.add(fsize13=new MenuItem("48"));
        font2.add(fsize14=new MenuItem("72"));
        mbar.add(format);
        about.add(item11=new MenuItem("About TextEditor"));
        mbar.add(about);
        item1.addActionListener(this);
        item2.addActionListener(this);
        item3.addActionListener(this);
        item4.addActionListener(this);
        item5.addActionListener(this);
        item6.addActionListener(this);
        item7.addActionListener(this);
        item8.addActionListener(this);
        item9.addActionListener(this);
        item10.addActionListener(this);
        item11.addActionListener(this);
        fname1.addActionListener(this);
        fname2.addActionListener(this);
        fname3.addActionListener(this);
        fname4.addActionListener(this);
        fname5.addActionListener(this);
        fname6.addActionListener(this);
        fstyle1.addActionListener(this);
        fstyle2.addActionListener(this);
        fstyle3.addActionListener(this);
        fstyle4.addActionListener(this);
        fsize1.addActionListener(this);
        fsize2.addActionListener(this);
        fsize3.addActionListener(this);
        fsize4.addActionListener(this);
        fsize5.addActionListener(this);
        fsize6.addActionListener(this);
        fsize7.addActionListener(this);
        fsize8.addActionListener(this);
        fsize9.addActionListener(this);
        fsize10.addActionListener(this);
        fsize11.addActionListener(this);
        fsize12.addActionListener(this);
        fsize13.addActionListener(this);
        fsize14.addActionListener(this);
        text=new TextArea(27,90);
        mainpanel.add(text);
        f=new Font("Monotype Corsiva",Font.PLAIN,15);
        text.setFont(f);
        this.setTitle("Untitled-TextEditor");
    }
    public void actionPerformed(ActionEvent ae)
    {
        command=(String)ae.getActionCommand();
        if(command.equals("New..."))
        {
            dispose();
            TextEditor note1 = new TextEditor("Untitled-TextEditor");
            note1.setSize(765,550);
            note1.setVisible(true);
            this.setTitle("Untitled-TextEditor");
        }
        try
        {
            if(command.equals("Open..."))
            {
                str4=" ";
                FileDialog dialog=new FileDialog(this,"Open");
                dialog.setVisible(true);
                str1=dialog.getDirectory();
                str2=dialog.getFile();
                str3=str1+str2;
                File f=new File(str3);
                FileInputStream fobj=new FileInputStream(f);
                len=(int)f.length();
                for(int j=0;j<len;j++)
                {
                    char str5=(char)fobj.read();
                    str4=str4 + str5;

                }
                fobj.close();
                text.setText(str4);
            }
            this.setTitle(str2);
        }
        catch(IOException e){}
        try
        {
            if(command.equals("Save As..."))
            {
                FileDialog dialog1=new FileDialog(this,"Save As",FileDialog.SAVE);
                dialog1.setVisible(true);
                str7=dialog1.getDirectory();
                str8=dialog1.getFile();
                str9=str7+str8;
                str6=text.getText();
                len1=str6.length();
                byte buf[]=str6.getBytes();
                File f1=new File(str9+".txt");
                FileOutputStream fobj1=new FileOutputStream(f1);
                for(int k=0;k<len1;k++)
                {
                    fobj1.write(buf[k]);
                }
                fobj1.close();
            }
            this.setTitle(str8);
        }
        catch(IOException e){}
        try
        {
            if(command.equals("   Save   "))
            {
                FileDialog dialog1=new FileDialog(this,"Save As",FileDialog.SAVE);
                dialog1.setVisible(true);
                str7=dialog1.getDirectory();
                str8=dialog1.getFile();
                str9=str7+str8;
                str6=text.getText();
                len1=str6.length();
                byte buf[]=str6.getBytes();
                File f1=new File(str9+".txt");
                FileOutputStream fobj1=new FileOutputStream(f1);
                for(int k=0;k<len1;k++)
                {
                    fobj1.write(buf[k]);
                }
                fobj1.close();
            }
            this.setTitle(str8);
        }
        catch(IOException e){}
        if(command.equals("Exit"))
        {
            str10=(str8.equals(" "))?"":("to "+str8+" ");
            JFrame frameExit = new JFrame("Do you want to save changes "+str10+"?");
            frameExit.setSize(400,160);
            frameExit.setVisible(true);
            JPanel panelE = new JPanel();
            frameExit.add(panelE);
            panelE.setLayout(new GridLayout(7,3));
            JLabel label1E = new JLabel("");
            JLabel label2E = new JLabel("");
            JLabel label3E = new JLabel("");
            JLabel labelL1 = new JLabel("");
            JLabel labelL2 = new JLabel("");
            JLabel labelL3 = new JLabel("");
            JLabel labelL4 = new JLabel("");
            JLabel labelL5 = new JLabel("");
            JLabel labelR1 = new JLabel("");
            JLabel labelR2 = new JLabel("");
            JLabel labelR3 = new JLabel("");
            JLabel labelR4 = new JLabel("");
            JLabel labelR5 = new JLabel("");
            JButton button1 = new JButton("   Save   ");
            JButton button2 = new JButton("Don't Save");
            panelE.add(labelL1);
            panelE.add(label1E);
            panelE.add(labelR1);
            panelE.add(labelL2);
            panelE.add(button1);
            panelE.add(labelR2);
            panelE.add(labelL3);
            panelE.add(label2E);
            panelE.add(labelR3);
            panelE.add(labelL4);
            panelE.add(button2);
            panelE.add(labelR4);
            panelE.add(labelL5);
            panelE.add(label3E);
            panelE.add(labelR5);
            button1.addActionListener (this); 
            button2.addActionListener (this); 
        }
        if(command.equals("Don't Save"))
        {
            System.exit(0);
        }
        if(command.equals("Cut (Ctrl+X)"))
        {
            str=text.getSelectedText();
            i=text.getText().indexOf(str);
            text.replaceRange(" ",i,i+str.length());
        }
        if(command.equals("Copy (Ctrl+C)"))
        {
            str=text.getSelectedText();
        }
        if(command.equals("Paste (Ctrl+V)"))
        {
            pos1=text.getCaretPosition();
            text.insert(str,pos1);
        }
        if(command.equals("Delete"))
        {
            String msg=text.getSelectedText();
            i=text.getText().indexOf(msg);
            text.replaceRange("",i,i+msg.length());
        }
        if(command.equals("Time/Date"))
        {
            gcalendar=new GregorianCalendar();
            String h=String.valueOf(gcalendar.get(Calendar.HOUR));
            String m=String.valueOf(gcalendar.get(Calendar.MINUTE));
            String s=String.valueOf(gcalendar.get(Calendar.SECOND));
            String date=String.valueOf(gcalendar.get(Calendar.DATE));
            String mon=months[gcalendar.get(Calendar.MONTH)];
            String year=String.valueOf(gcalendar.get(Calendar.YEAR));
            String hms="Time"+" - "+h+":"+m+":"+s+" Date"+" - "+date+" "+mon+" "+year;
            int loc=text.getCaretPosition();
            text.insert(hms,loc);
        }
        if(command.equals("Courier"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font("Courier",fontStyle,fontSize);
            text.setFont(f);
        }
        if(command.equals("Helvetica"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font("Helvetica",fontStyle,fontSize);
            text.setFont(f);
        }
        if(command.equals("Monospaced"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font("Monospaced",fontStyle,fontSize);
            text.setFont(f);
        }
        if(command.equals("Serif"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font("Serif",fontStyle,fontSize);
            text.setFont(f);
        }
        if(command.equals("SansSerif"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font("SansSerif",fontStyle,fontSize);
            text.setFont(f);
        }
        if(command.equals("TimesRoman"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font("TimesRoman",fontStyle,fontSize);
            text.setFont(f);
        }     
        if(command.equals("Regular"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,Font.PLAIN,fontSize);
            text.setFont(f);
        }
        if(command.equals("Bold"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,Font.BOLD,fontSize);
            text.setFont(f);
        }
        if(command.equals("Italic"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,Font.ITALIC,fontSize);
            text.setFont(f);
        }
        if(command.equals("Bold Italic"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,Font.BOLD|Font.ITALIC,fontSize);
            text.setFont(f);
        }
        if(command.equals("8"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,fontStyle,8);
            text.setFont(f);
        }
        if(command.equals("10"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,fontStyle,10);
            text.setFont(f);
        }
        if(command.equals("12"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,fontStyle,12);
            text.setFont(f);
        }
        if(command.equals("14"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,fontStyle,14);
            text.setFont(f);
        }
        if(command.equals("16"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,fontStyle,16);
            text.setFont(f);
        }
        if(command.equals("18"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,fontStyle,18);
            text.setFont(f);
        }
        if(command.equals("20"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();

            f=new Font(fontName,fontStyle,20);
            text.setFont(f);
        }
        if(command.equals("22"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,fontStyle,22);
            text.setFont(f);
        }
        if(command.equals("24"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,fontStyle,24);
            text.setFont(f);
        }
        if(command.equals("26"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,fontStyle,26);
            text.setFont(f);
        }
        if(command.equals("28"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,fontStyle,28);
            text.setFont(f);
        }
        if(command.equals("36"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,fontStyle,36);
            text.setFont(f);
        }
        if(command.equals("48"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,fontStyle,48);
            text.setFont(f);
        }
        if(command.equals("72"))
        {
            String fontName=f.getName();
            String fontFamily=f.getFamily();
            int fontSize=f.getSize();
            int fontStyle=f.getStyle();
            f=new Font(fontName,fontStyle,72);
            text.setFont(f);
        }     
        if(command.equals("Select All (Ctrl+A)"))
        {
            String strText=text.getText();
            int strLen=strText.length();
            text.select(0,strLen);
        }
        if(command.equals("About TextEditor"))
        {
            JFrame frameAbout = new JFrame("About TextEditor");
            frameAbout.setSize(400,260);
            frameAbout.setVisible(true);
            JPanel panel = new JPanel();
            frameAbout.add(panel);
            panel.setLayout(new GridLayout(10,1));
            JLabel label1 = new JLabel("");
            JLabel label2 = new JLabel("");
            JLabel label3 = new JLabel("");
            JLabel label4 = new JLabel("");
            JLabel label5 = new JLabel("            TextEditor");
            JLabel label6 = new JLabel("            Version 1.0");
            JLabel label7 = new JLabel("");
            JLabel label8 = new JLabel("            This application has been developed by VoidCode.");
            JLabel label9 = new JLabel("            All rights reserved.");
            JLabel label10 = new JLabel("");
            panel.add(label1);
            panel.add(label2);
            panel.add(label3);
            panel.add(label4);
            panel.add(label5);
            panel.add(label6);
            panel.add(label7);
            panel.add(label8);
            panel.add(label9);
            panel.add(label10);
        }
    }
    public static void main(String args[])
    {
        TextEditor note = new TextEditor("Untitled-TextEditor");
        note.setSize(765,550);
        note.setVisible(true);
    }
}

Output:

TextEditor by VoidCode (1)
TextEditor by VoidCode (2)

Advice:

You can copy the above program and paste it in any offline compiler (eg. BlueJ). To make a .jar file, create this class in a new project and convert it to .jar file. The .jar file can be converted to a .exe (windows executable) file using any suitable wrapper application (eg. Launch4J). Don’t forget to add an iconic logo.

Here are a few logos to try. Convert the .jpg file to .ico using any online converter before wrapping the .jar file to .exe file.

Logo (1)
Logo (2)
Logo (3)
Logo (4)

NOTE : In the next update, we may add some more features to the text editor, such as, Page Setup, Print, Undo, Replace, Go To, Word Wrap, Status Bar & Help options (In this version, the Undo feature is available as keyboard shortcut Ctrl+Z).

Hope this helps. Team Void Code 🙂

Leave a comment