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.
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.
Java JDK (8 or above)
Any IDE like Eclipse, IntelliJ IDEA, or NetBeans
Java Swing provides GUI components like JFrame, JTextArea, JMenuBar, etc.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
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);
}
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.
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;
}
}
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)
.
Add a main()
method to start the app.
public static void main(String[] args) {
new SimpleNotepad(); // Create instance to show GUI
}
}
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();
}
}
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
Thank you for visiting! Enjoy exploring our diverse collection of blogs, crafted with passion and insight to inspire and inform. Happy reading!