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
- Ensure you have Java installed on your system. You can check this by running
java -version
in your terminal or command prompt. - Create a new file named
SimpleTextEditor.java
and paste the code above into this file. - Compile the Java program by running
javac SimpleTextEditor.java
in your terminal or command prompt. - Run the program using the command
java SimpleTextEditor
. - 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.