Google Advertisement

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

Google Advertisement
πŸ”₯ Read with Full Features on Our Website

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.

Published on 04 May 2025
By Vandu

Design 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.

πŸ”₯ Read with Full Features on Our Website

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

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


πŸ”§ Tools Required


πŸ“¦ 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:


🧠 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:


πŸš€ 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:

❀️ Like πŸ’¬ Comment πŸ”— Share
Google Advertisement
πŸ‘‰ View Full Version on Main Website β†—
Google Advertisement
πŸ‘‰ Read Full Article on Website