Java Swing | Create a simple text editor

Last Updated : 6 Oct, 2026

Creating a simple text editor in Java Swing involves using GUI components to handle text input and user actions. It typically uses a JTextArea for editing and menu components to perform operations.

  • JTextArea is used to display and edit text content.
  • JMenuBar, JMenu, and JMenuItem provide options like open, save, and edit.
  • ActionListener is used to handle user actions on menu items.

The text editor contains a menu bar with two menus, File and Edit, along with a Close menu item placed directly on the menu bar.File menu

  • Open: This menu item is used to open an existing file from the system.
  • Save: This menu item is used to save the current file.
  • Print: This menu item is used to print the contents of the text area.
  • New: This menu item is used to create a new blank file.

Edit menu

  • Cut: This menu item is used to remove the selected text and copy it to the clipboard.
  • Copy: This menu item is used to copy the selected text to the clipboard.
  • Paste: This menu item is used to paste text from the clipboard into the text area.

Close Menu Item

  • Close: Closes the text editor window.

We have used file reader and file writer for more information on file reading and writing in Java. Please follow the below links: 

Methods used

MethodExplanation
cut()Removes the selected text from the text area and places it on the clipboard.
copy()Copies the selected text from the text area to the clipboard without removing it.
paste()Inserts the clipboard content at the caret position or replaces the currently selected text.
print()Prints the content of the text area.

Steps to Create a Simple Text Editor:

Step 1: Create Frame & Apply Theme

Create a JFrame titled Editor and apply Metal Look & Feel with Ocean Theme.

Step 2: Add Text Area

Create JTextArea and wrap it inside JScrollPane for scrolling.

Step 3: Create Menu Bar

Create a JMenuBar and add the File and Edit menus. A separate Close menu item is also added directly to the menu bar.

Step 4: Add Menu Items

  • File -> New, Open, Save, Print
  • Edit -> Cut, Copy, Paste
  • Menu Bar -> Close

Step 5: Attach ActionListener

Attach ActionListener to each menu item to handle actions.

Step 6: Add Components

  • Add menu items -> menus -> menu bar -> frame
  • Add text area (scroll pane) to frame

Step 7: Set Frame Properties

Set size 500×500, default close operation, and make frame visible

How the functions of the menu will be invoked:

  • Cut, Copy, Paste, Print: Invoke built-in JTextArea methods (cut(), copy(), paste(), print()).
  • Open: Opens a file chooser, reads the selected file, and displays its content in the text area.
  • Save: Opens a file chooser and writes the text area content to the selected file.
  • New: Clears the text area.
  • Close: Closes the application window.

Program: 

Java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.plaf.metal.*;

class Editor extends JFrame implements ActionListener {

    JTextArea t;
    JFrame f;

    Editor()
    {
        f = new JFrame("Editor");

        try {
            UIManager.setLookAndFeel(
                "javax.swing.plaf.metal.MetalLookAndFeel");
            MetalLookAndFeel.setCurrentTheme(
                new OceanTheme());
        }
        catch (Exception e) {
            // Use the default look and feel
        }

        // Text area with scroll
        t = new JTextArea();
        JScrollPane sp = new JScrollPane(t);

        // Menu bar
        JMenuBar mb = new JMenuBar();

        // File menu
        JMenu file = new JMenu("File");

        JMenuItem newFile = new JMenuItem("New");
        JMenuItem open = new JMenuItem("Open");
        JMenuItem save = new JMenuItem("Save");
        JMenuItem print = new JMenuItem("Print");

        newFile.addActionListener(this);
        open.addActionListener(this);
        save.addActionListener(this);
        print.addActionListener(this);

        file.add(newFile);
        file.add(open);
        file.add(save);
        file.add(print);

        // Edit menu
        JMenu edit = new JMenu("Edit");

        JMenuItem cut = new JMenuItem("Cut");
        JMenuItem copy = new JMenuItem("Copy");
        JMenuItem paste = new JMenuItem("Paste");

        cut.addActionListener(this);
        copy.addActionListener(this);
        paste.addActionListener(this);

        edit.add(cut);
        edit.add(copy);
        edit.add(paste);

        // Close menu item
        JMenuItem close = new JMenuItem("Close");
        close.addActionListener(this);

        mb.add(file);
        mb.add(edit);
        mb.add(close);

        f.setJMenuBar(mb);
        f.add(sp);

        f.setSize(500, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        String s = e.getActionCommand();

        try {
            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")) {
                JFileChooser j = new JFileChooser();

                int r = j.showSaveDialog(this);

                if (r == JFileChooser.APPROVE_OPTION) {
                    File file = j.getSelectedFile();

                    try (BufferedWriter w
                         = new BufferedWriter(
                             new FileWriter(file))) {
                        w.write(t.getText());
                    }
                }
            }
            else if (s.equals("Print")) {
                t.print();
            }
            else if (s.equals("Open")) {
                JFileChooser j = new JFileChooser();

                int r = j.showOpenDialog(this);

                if (r == JFileChooser.APPROVE_OPTION) {
                    File file = j.getSelectedFile();

                    try (BufferedReader br
                         = new BufferedReader(
                             new FileReader(file))) {
                        t.read(br, null);
                    }
                }
            }
            else if (s.equals("New")) {
                t.setText("");
            }
            else if (s.equals("Close")) {
                f.dispose();
            }
        }
        catch (Exception ex) {
            JOptionPane.showMessageDialog(this,
                                          ex.getMessage());
        }
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(Editor::new);
    }
}

Output: 

Explanation: A simple text editor GUI window with a basic menu bar (File, Edit, Close) and an empty workspace.

Explanation: A simple text editor displaying sample text content with a basic menu bar for file operations.

Explanation: A text editor with an open file dialog box, allowing the user to browse and select files from the system.

Explanation: A text editor with a print dialog box, allowing the user to configure printer settings and print the document.

Note: The above programs might not run in an online IDE please use an offline compiler.

Comment