Create a Simple Text Editor in Java Using Swing | Step-by-Step Guide

By Vandu
May 4, 2025

Follow us on


Learn how to create a basic notepad-like text editor in Java using Swing. Follow this beginner-friendly tutorial with full code, GUI, and clear explanations.

Create a Simple Text Editor in Java Using Swing | Step-by-Step GuideDesign a Basic Notepad-like App Using Swing in Java

Creating a notepad-like text editor is a great project for Java beginners. It teaches you about GUI development, event handling, file operations, and how to use Java Swing components.

In this tutorial, we’ll build a simple Notepad app that lets you:

  • Write text

  • Open a .txt file

  • Save text to a file

  • Exit the application

Let’s dive into the code and learn step by step.


🔧 Tools Required

  • Java JDK (8 or above)

  • Any IDE like Eclipse, IntelliJ IDEA, or NetBeans


📦 Step 1: Import Required Swing Packages

Java Swing provides GUI components like JFrame, JTextArea, JMenuBar, etc.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
 

🧱 Step 2: Create the Notepad Class and GUI Layout

public class SimpleNotepad extends JFrame implements ActionListener {
    
    JTextArea textArea;
    JFrame frame;
    
    public SimpleNotepad() {
        // Frame setup
        frame = new JFrame("Simple Notepad");
        frame.setSize(600, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // Text area
        textArea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(textArea);
        frame.add(scrollPane);
        
        // Menu bar
        JMenuBar menuBar = new JMenuBar();
        
        // File menu
        JMenu fileMenu = new JMenu("File");
        JMenuItem open = new JMenuItem("Open");
        JMenuItem save = new JMenuItem("Save");
        JMenuItem exit = new JMenuItem("Exit");

        open.addActionListener(this);
        save.addActionListener(this);
        exit.addActionListener(this);

        fileMenu.add(open);
        fileMenu.add(save);
        fileMenu.add(exit);
        
        menuBar.add(fileMenu);
        frame.setJMenuBar(menuBar);
        
        // Show frame
        frame.setVisible(true);
    }
 

Explanation:

  • We extend JFrame to create a window.

  • JTextArea is where users can type.

  • JMenuBar adds a File menu with "Open", "Save", and "Exit" options.

  • We use ActionListener to handle clicks.


🧠 Step 3: Add Functionality (Open, Save, Exit)

Now let’s add logic inside actionPerformed() method.

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        
        switch (command) {
            case "Open":
                JFileChooser fileChooser = new JFileChooser();
                int openResult = fileChooser.showOpenDialog(frame);
                if (openResult == JFileChooser.APPROVE_OPTION) {
                    File file = fileChooser.getSelectedFile();
                    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
                        textArea.setText(""); // Clear existing text
                        String line;
                        while ((line = br.readLine()) != null) {
                            textArea.append(line + "\n");
                        }
                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(frame, "File Not Found!");
                    }
                }
                break;

            case "Save":
                JFileChooser saveChooser = new JFileChooser();
                int saveResult = saveChooser.showSaveDialog(frame);
                if (saveResult == JFileChooser.APPROVE_OPTION) {
                    File file = saveChooser.getSelectedFile();
                    try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
                        bw.write(textArea.getText());
                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(frame, "Error Saving File!");
                    }
                }
                break;

            case "Exit":
                System.exit(0);
                break;
        }
    }
 

Explanation:

  • Open: Uses JFileChooser to select a file, reads it line by line, and displays it in the text area.

  • Save: Lets you choose where to save the text using BufferedWriter.

  • Exit: Closes the app with System.exit(0).


🚀 Step 4: Run the Application

Add a main() method to start the app.

    public static void main(String[] args) {
        new SimpleNotepad(); // Create instance to show GUI
    }
}
 

Final Code: Copy-Paste and Run

import javax.swing.*;
import java.awt.event.*;
import java.io.*;

public class SimpleNotepad extends JFrame implements ActionListener {
    
    JTextArea textArea;
    JFrame frame;
    
    public SimpleNotepad() {
        frame = new JFrame("Simple Notepad");
        frame.setSize(600, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        textArea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(textArea);
        frame.add(scrollPane);
        
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenuItem open = new JMenuItem("Open");
        JMenuItem save = new JMenuItem("Save");
        JMenuItem exit = new JMenuItem("Exit");

        open.addActionListener(this);
        save.addActionListener(this);
        exit.addActionListener(this);

        fileMenu.add(open);
        fileMenu.add(save);
        fileMenu.add(exit);
        menuBar.add(fileMenu);
        frame.setJMenuBar(menuBar);
        
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        
        switch (command) {
            case "Open":
                JFileChooser fileChooser = new JFileChooser();
                int openResult = fileChooser.showOpenDialog(frame);
                if (openResult == JFileChooser.APPROVE_OPTION) {
                    File file = fileChooser.getSelectedFile();
                    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
                        textArea.setText("");
                        String line;
                        while ((line = br.readLine()) != null) {
                            textArea.append(line + "\n");
                        }
                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(frame, "File Not Found!");
                    }
                }
                break;

            case "Save":
                JFileChooser saveChooser = new JFileChooser();
                int saveResult = saveChooser.showSaveDialog(frame);
                if (saveResult == JFileChooser.APPROVE_OPTION) {
                    File file = saveChooser.getSelectedFile();
                    try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
                        bw.write(textArea.getText());
                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(frame, "Error Saving File!");
                    }
                }
                break;

            case "Exit":
                System.exit(0);
                break;
        }
    }

    public static void main(String[] args) {
        new SimpleNotepad();
    }
}
 

🏁 Conclusion

You’ve now built a working Notepad-like text editor using Java Swing! This project teaches important concepts like GUI design, event handling, and file I/O.

You can enhance it further by:

  • Adding Find & Replace

  • Adding Font customization

  • Adding Cut/Copy/Paste options


© 2025 Pay18News. All rights reserved.