Skip to content

📝 “Build a Simple Notepad in Java (With Save & Open!) — Better Than Windows Notepad?!”

Tired of plain old Notepad? Let’s build your own version — in just one Java file!
This beginner-friendly project is perfect for practicing Java Swing, file handling, and event-driven programming.
By the end, you’ll have a fully functional text editor with a modern feel!


🧠 What You’ll Learn:

  • Create a basic GUI with JFrame and JTextArea
  • Use a JMenuBar for File operations like Open, Save, New, and Exit
  • Handle text files with BufferedReader and BufferedWriter
  • Center and resize your app window

📦 Full Java Code: SimpleNotepad.java

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

public class SimpleNotepad extends JFrame implements ActionListener {

    JTextArea textArea;
    JFileChooser fileChooser;
    File currentFile;

    public SimpleNotepad() {
        setTitle("Java Notepad - Devesh Edition");
        setSize(800, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // Center the window

        textArea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(textArea);
        add(scrollPane);

        createMenuBar();

        fileChooser = new JFileChooser();

        setVisible(true);
    }

    private void createMenuBar() {
        JMenuBar menuBar = new JMenuBar();

        JMenu fileMenu = new JMenu("File");

        JMenuItem newItem = new JMenuItem("New");
        JMenuItem openItem = new JMenuItem("Open...");
        JMenuItem saveItem = new JMenuItem("Save");
        JMenuItem exitItem = new JMenuItem("Exit");

        newItem.addActionListener(this);
        openItem.addActionListener(this);
        saveItem.addActionListener(this);
        exitItem.addActionListener(this);

        fileMenu.add(newItem);
        fileMenu.add(openItem);
        fileMenu.add(saveItem);
        fileMenu.addSeparator();
        fileMenu.add(exitItem);

        menuBar.add(fileMenu);
        setJMenuBar(menuBar);
    }

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

        switch (command) {
            case "New":
                textArea.setText("");
                currentFile = null;
                setTitle("Java Notepad - New File");
                break;

            case "Open...":
                int openResult = fileChooser.showOpenDialog(this);
                if (openResult == JFileChooser.APPROVE_OPTION) {
                    currentFile = fileChooser.getSelectedFile();
                    openFile(currentFile);
                }
                break;

            case "Save":
                if (currentFile == null) {
                    int saveResult = fileChooser.showSaveDialog(this);
                    if (saveResult == JFileChooser.APPROVE_OPTION) {
                        currentFile = fileChooser.getSelectedFile();
                    }
                }
                if (currentFile != null) {
                    saveFile(currentFile);
                }
                break;

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

    private void openFile(File file) {
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            textArea.read(reader, null);
            setTitle("Java Notepad - " + file.getName());
        } catch (IOException ex) {
            showError("Could not open file.");
        }
    }

    private void saveFile(File file) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
            textArea.write(writer);
            setTitle("Java Notepad - " + file.getName());
        } catch (IOException ex) {
            showError("Could not save file.");
        }
    }

    private void showError(String message) {
        JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE);
    }

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

🔍 Line-by-Line Breakdown

🖼️ GUI Setup

public class SimpleNotepad extends JFrame implements ActionListener {

This defines our main class, extending JFrame (for the GUI window) and implementing ActionListener (for handling menu actions)

JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane);

Creates a large text editing area with scrollbars.

setTitle("Java Notepad - Devesh Edition");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);

Basic window setup — sets title, size, close behavior, and centers the window on screen.

📂 File Menu & Items

JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");

Creates a menu bar with a File menu.

JMenuItem newItem = new JMenuItem("New");
newItem.addActionListener(this);

Adds menu items and connects them to the main class via ActionListener.

🎯 Action Handler

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

This method is triggered whenever a menu item is clicked. It checks the label (command) to decide what to do.

case "New":
    textArea.setText(""); // Clear text

Resets the editor for a new file.

case "Open...":
    fileChooser.showOpenDialog(this);
    openFile(file);

Opens a file using JFileChooser and loads its contents into the editor.

case "Save":
    fileChooser.showSaveDialog(this);
    saveFile(file);

Lets the user pick a location and saves the text to the file.

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

Closes the app.

🧠 Helper Methods


private void openFile(File file)
private void saveFile(File file)
private void showError(String message)

Reads the file line by line and loads it into the text area.

Writes the current text to the chosen file.

Displays an error pop-up if something goes wrong.

🖼️ Final Result

Here’s what your Notepad app will look like


🌟 Bonus Challenges

✅ Add:

  • Word counter at bottom
  • Dark/light theme toggle
  • Auto-save every 30 seconds
  • Tabbed interface for multiple files

🏁 Conclusion

Boom! 💥 In just 100 lines of Java, you’ve built your own Notepad app — with full file support!
You now know:

  • How to work with Java Swing UI
  • How to handle file I/O safely
  • How to make desktop apps with real-world use
Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *