Java

 

Learn how to create a basic text editor using Java programming. This step-by-step guide explains how to build a text editor with essential functionality like creating, opening, saving, and editing text files.

Objective

The objective of this tutorial is to guide you through the process of building a simple text editor application using Java. By the end of this project, you will have a functional text editor with the ability to create, edit, save, and load text files.

Code for Simple Text Editor

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

public class SimpleTextEditor {
    private JFrame frame;
    private JTextArea textArea;
    private JFileChooser fileChooser;

    public SimpleTextEditor() {
        frame = new JFrame("Simple Text Editor");
        textArea = new JTextArea();
        fileChooser = new JFileChooser();
        
        // Setup frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        
        // Setup text area
        textArea.setFont(new Font("Arial", Font.PLAIN, 14));
        JScrollPane scrollPane = new JScrollPane(textArea);
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        
        // Create menu bar
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        
        // Create menu items
        JMenuItem newItem = new JMenuItem("New");
        newItem.addActionListener(e -> textArea.setText(""));
        
        JMenuItem openItem = new JMenuItem("Open");
        openItem.addActionListener(e -> openFile());
        
        JMenuItem saveItem = new JMenuItem("Save");
        saveItem.addActionListener(e -> saveFile());
        
        JMenuItem exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(e -> System.exit(0));
        
        fileMenu.add(newItem);
        fileMenu.add(openItem);
        fileMenu.add(saveItem);
        fileMenu.add(exitItem);
        
        menuBar.add(fileMenu);
        frame.setJMenuBar(menuBar);
        
        // Final setup
        frame.setVisible(true);
    }

    private void openFile() {
        int returnValue = fileChooser.showOpenDialog(frame);
        if (returnValue == 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 e) {
                JOptionPane.showMessageDialog(frame, "Error opening file", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void saveFile() {
        int returnValue = fileChooser.showSaveDialog(frame);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
                textArea.write(bw);
            } catch (IOException e) {
                JOptionPane.showMessageDialog(frame, "Error saving file", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new SimpleTextEditor());
    }
}

Explanation of the Program Structure

This program uses Java Swing components to create a simple GUI-based text editor. Below is a breakdown of the main components of the program:

  • JFrame: The main window of the application.
  • JTextArea: A component that allows the user to input and edit text.
  • JFileChooser: A dialog that allows users to choose files to open or save.
  • JMenuBar: Contains a menu with options like “New”, “Open”, “Save”, and “Exit”.

The code contains key methods such as openFile() and saveFile() to load and save files, respectively. The openFile() method uses a BufferedReader to read a text file, while the saveFile() method uses a BufferedWriter to save the content of the text area to a file.

How to Run the Program

  1. Ensure you have Java installed on your system. You can check this by running java -version in your terminal or command prompt.
  2. Create a new file named SimpleTextEditor.java and paste the code above into this file.
  3. Compile the Java program by running javac SimpleTextEditor.java in your terminal or command prompt.
  4. Run the program using the command java SimpleTextEditor.
  5. The simple text editor window will open, and you can use the options in the menu bar to create, open, save, and edit text files.
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)