Text Editor In Java Project With Source Code

Text Editor In Java Project With Source Code

The Text Editor In Java Project was developed in JAVA Programming using NetBeans IDE, In this Simple Java Project With Source Code is a simple text editor in Java Swing we will use a JTextArea, a JMenuBar and add JMenu to it and we will add JMenuItems. All the menu items will have actionListener to detect any action.

A Text Editor For Java Programming is a computer program that lets a user enter, change, store, and usually print text (characters and numbers, each encoded by the computer and its input and output devices, arranged to have meaning to users or to other programs).

There will be a menu bar and it will contain two menus and a button: 

  1. File menu
    • open: this menuitem is used to open a file
    • save: this menuitem is used to save a file
    • print : this menuitem is used to print the components of the text area
    • new : this menuitem is used to create a new blank file
  2. Edit menu
    • cut: this menuitem is to cut the selected area and copy it to clipboard
    • copy: this menuitem is to copy the selected area to the clipboard
    • paste : this menuitem is to paste the text from the clipboard to the text area
  3. Close : this button closes the frame

This JAVA Project also includes a Java Project Free Download Source Code, just find the downloadable source code below and click to start downloading.

Text Editor in Java with Source Cod...
Text Editor in Java with Source Code 2021 | Java Projects with Source Code [FREE DOWNLOAD]

To start executing a Text Editor In Java Project With Source Code, makes sure that you have  NeatBeans IDE or any platform of Java installed in your computer.

Text Editor In Java Project With Source Code : Steps on how to run the project

Time needed: 5 minutes.

These are the steps on how to run Text Editor In Java Project With Source Code

  • Step 1: Download source code.

    First, download the source code given below.
    button

  • Step 2: Extract file.

    Second, after you finished download the source code, extract the zip file.
    Text Editor zip file

  • Step 3: Open Netbeans.

    Third, open “Netbeans IDE”.
    Text Editor open netbeans

  • Step 4: Click open project.

    Fourth, click open project and choose your download source code.
    Text Editor open project

  • Step 5: Run the project.

    Fifth, right click the project folder and click run.
    Text Editor run project

Project Source Code

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author jude
 */
// Java Program to create a text editor using java
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.plaf.metal.*;
import javax.swing.text.*;
class editor extends JFrame implements ActionListener {
    // Text component
    JTextArea t;
 
    // Frame
    JFrame f;
 
    // Constructor
    editor()
    {
        // Create a frame
        f = new JFrame("editor");
 
        try {
            // Set metal look and feel
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
 
            // Set theme to ocean
            MetalLookAndFeel.setCurrentTheme(new OceanTheme());
        }
        catch (Exception e) {
        }
 
        // Text component
        t = new JTextArea();
 
        // Create a menubar
        JMenuBar mb = new JMenuBar();
 
        // Create amenu for menu
        JMenu m1 = new JMenu("File");
 
        // Create menu items
        JMenuItem mi1 = new JMenuItem("New");
        JMenuItem mi2 = new JMenuItem("Open");
        JMenuItem mi3 = new JMenuItem("Save");
        JMenuItem mi9 = new JMenuItem("Print");
 
        // Add action listener
        mi1.addActionListener(this);
        mi2.addActionListener(this);
        mi3.addActionListener(this);
        mi9.addActionListener(this);
 
        m1.add(mi1);
        m1.add(mi2);
        m1.add(mi3);
        m1.add(mi9);
 
        // Create amenu for menu
        JMenu m2 = new JMenu("Edit");
 
        // Create menu items
        JMenuItem mi4 = new JMenuItem("cut");
        JMenuItem mi5 = new JMenuItem("copy");
        JMenuItem mi6 = new JMenuItem("paste");
 
        // Add action listener
        mi4.addActionListener(this);
        mi5.addActionListener(this);
        mi6.addActionListener(this);
 
        m2.add(mi4);
        m2.add(mi5);
        m2.add(mi6);
 
        JMenuItem mc = new JMenuItem("close");
 
        mc.addActionListener(this);
 
        mb.add(m1);
        mb.add(m2);
        mb.add(mc);
 
        f.setJMenuBar(mb);
        f.add(t);
        f.setSize(500, 500);
        f.show();
    }
 
    // If a button is pressed
    public void actionPerformed(ActionEvent e)
    {
        String s = e.getActionCommand();
 
        if (s.equals("cut")) {
            t.cut();
        }
        else if (s.equals("copy")) {
            t.copy();
        }
        else if (s.equals("paste")) {
            t.paste();
        }
        else if (s.equals("Save")) {
            // Create an object of JFileChooser class
            JFileChooser j = new JFileChooser("f:");
 
            // Invoke the showsSaveDialog function to show the save dialog
            int r = j.showSaveDialog(null);
 
            if (r == JFileChooser.APPROVE_OPTION) {
 
                // Set the label to the path of the selected directory
                File fi = new File(j.getSelectedFile().getAbsolutePath());
 
                try {
                    // Create a file writer
                    FileWriter wr = new FileWriter(fi, false);
 
                    // Create buffered writer to write
                    BufferedWriter w = new BufferedWriter(wr);
 
                    // Write
                    w.write(t.getText());
 
                    w.flush();
                    w.close();
                }
                catch (Exception evt) {
                    JOptionPane.showMessageDialog(f, evt.getMessage());
                }
            }
            // If the user cancelled the operation
            else
                JOptionPane.showMessageDialog(f, "the user cancelled the operation");
        }
        else if (s.equals("Print")) {
            try {
                // print the file
                t.print();
            }
            catch (Exception evt) {
                JOptionPane.showMessageDialog(f, evt.getMessage());
            }
        }
        else if (s.equals("Open")) {
            // Create an object of JFileChooser class
            JFileChooser j = new JFileChooser("f:");
 
            // Invoke the showsOpenDialog function to show the save dialog
            int r = j.showOpenDialog(null);
 
            // If the user selects a file
            if (r == JFileChooser.APPROVE_OPTION) {
                // Set the label to the path of the selected directory
                File fi = new File(j.getSelectedFile().getAbsolutePath());
 
                try {
                    // String
                    String s1 = "", sl = "";
 
                    // File reader
                    FileReader fr = new FileReader(fi);
 
                    // Buffered reader
                    BufferedReader br = new BufferedReader(fr);
 
                    // Initialize sl
                    sl = br.readLine();
 
                    // Take the input from the file
                    while ((s1 = br.readLine()) != null) {
                        sl = sl + "\n" + s1;
                    }
 
                    // Set the text
                    t.setText(sl);
                }
                catch (Exception evt) {
                    JOptionPane.showMessageDialog(f, evt.getMessage());
                }
            }
            // If the user cancelled the operation
            else
                JOptionPane.showMessageDialog(f, "the user cancelled the operation");
        }
        else if (s.equals("New")) {
            t.setText("");
        }
        else if (s.equals("close")) {
            f.setVisible(false);
        }
    }
 
    // Main class
    public static void main(String args[])
    {
        editor e = new editor();
    }
}  

Project Output

Text Editor In Java Project Output 1
Text Editor In Java Project Output 1
Text Editor In Java Project Output 2
Text Editor In Java Project Output 2

About The Text Editor In Java

Project Name:Text Editor
Language/s Used:JAVA
Database:None
Type:Desktop Application
Developer:IT SOURCECODE
Updates:0
Text Editor In Java– Project Information

Downloadable Source Code Below.

Anyway, if you want to level up your programming knowledge, especially Java, try this new article I’ve made for you Best Java Projects With Source Code For Beginners Free Download 2021.

Summary

The Java Project With Source Code is built fully in Java using Netbeans IDE. It has full-featured Graphical User Interface (GUI) with all the functionalities

This Article is the way to enhance and develop our skills and logic ideas which is important in practicing the Java programming language which is most well known and most usable programming language in many company.

This Simple Project also includes a downloadable source code for free.

Inquiries

If you have any questions or suggestions about Text Editor In Java Project With Source Code, please feel free to leave a comment below.

1 thought on “Text Editor In Java Project With Source Code”

Leave a Comment