Creating a simple text editor in Java Swing involves using GUI components to handle text input and user actions. It typically uses a JTextArea for editing and menu components to perform operations.
- JTextArea is used to display and edit text content.
- JMenuBar, JMenu, and JMenuItem provide options like open, save, and edit.
- ActionListener is used to handle user actions on menu items.
There will be a menu bar and it will contain two menus and a button:
File menu
- Open: This menu item is used to open an existing file from the system.
- Save: This menu item is used to save the current file.
- Print: This menu item is used to print the contents of the text area.
- New: This menu item is used to create a new blank file.
Edit menu
- Cut: This menu item is used to remove the selected text and copy it to the clipboard.
- Copy: This menu item is used to copy the selected text to the clipboard.
- Paste: This menu item is used to paste text from the clipboard into the text area.
- Close: This menu item is used to close the application window.
We have used file reader and file writer for more information on file reading and writing in Java. Please follow the below links:
Methods used
| method | explanation |
|---|---|
| cut() | removes the selected area from the text area and store it in clipboard |
| copy() | copies the selected area from the text area and store it in clipboard |
| paste() | removes the selected area from the text area and store it in clipboard |
| print() | prints the components of the text area |
Steps to Create a Simple Text Editor:
Step 1: Create Frame & Apply Theme
Create a JFrame titled Editor and apply Metal Look & Feel with Ocean Theme.
Step 2: Add Text Area
Create JTextArea and wrap it inside JScrollPane for scrolling.
Step 3: Create Menu Bar
Create JMenuBar and add menus: File, Edit, Close.
Step 4: Add Menu Items
- File -> New, Open, Save, Print
- Edit -> Cut, Copy, Paste
- Close -> Exit option
Step 5: Attach ActionListener
Attach ActionListener to each menu item to handle actions.
Step 6: Add Components
- Add menu items -> menus -> menu bar -> frame
- Add text area (scroll pane) to frame
Step 7: Set Frame Properties
Set size 500×500, default close operation, and make frame visible
How the functions of the menu will be invoked:
- Cut, Copy, Paste, Print: Invoke built-in JTextArea methods (cut(), copy(), paste(), print()).
- Open: Opens a file chooser, reads the selected file, and displays its content in the text area.
- Save: Opens a file chooser and writes the text area content to the selected file.
- New: Clears the text area.
- Close: Closes the application window.
Program:
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.plaf.metal.*;
class Editor extends JFrame implements ActionListener {
JTextArea t;
JFrame f;
Editor() {
// Create frame
f = new JFrame("Editor");
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
MetalLookAndFeel.setCurrentTheme(new OceanTheme());
} catch (Exception e) {}
// Text Area with Scroll
t = new JTextArea();
JScrollPane sp = new JScrollPane(t);
// Menu Bar
JMenuBar mb = new JMenuBar();
// File Menu
JMenu file = new JMenu("File");
JMenuItem newFile = new JMenuItem("New");
JMenuItem open = new JMenuItem("Open");
JMenuItem save = new JMenuItem("Save");
JMenuItem print = new JMenuItem("Print");
newFile.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
print.addActionListener(this);
file.add(newFile);
file.add(open);
file.add(save);
file.add(print);
// Edit Menu
JMenu edit = new JMenu("Edit");
JMenuItem cut = new JMenuItem("Cut");
JMenuItem copy = new JMenuItem("Copy");
JMenuItem paste = new JMenuItem("Paste");
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
edit.add(cut);
edit.add(copy);
edit.add(paste);
// Close
JMenuItem close = new JMenuItem("Close");
close.addActionListener(this);
mb.add(file);
mb.add(edit);
mb.add(close);
f.setJMenuBar(mb);
f.add(sp);
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
try {
if (s.equals("Cut")) {
t.cut();
} else if (s.equals("Copy")) {
t.copy();
} else if (s.equals("Paste")) {
t.paste();
} else if (s.equals("Save")) {
JFileChooser j = new JFileChooser();
int r = j.showSaveDialog(this);
if (r == JFileChooser.APPROVE_OPTION) {
File file = j.getSelectedFile();
BufferedWriter w = new BufferedWriter(new FileWriter(file));
w.write(t.getText());
w.close();
}
} else if (s.equals("Print")) {
t.print();
} else if (s.equals("Open")) {
JFileChooser j = new JFileChooser();
int r = j.showOpenDialog(this);
if (r == JFileChooser.APPROVE_OPTION) {
File file = j.getSelectedFile();
BufferedReader br = new BufferedReader(new FileReader(file));
t.read(br, null);
br.close();
}
} else if (s.equals("New")) {
t.setText("");
} else if (s.equals("Close")) {
f.dispose();
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
public static void main(String[] args) {
new Editor();
}
}
Output:

Explanation: A simple text editor GUI window with a basic menu bar (File, Edit, Close) and an empty workspace.

Explanation: A simple text editor displaying sample text content with a basic menu bar for file operations.

Explanation: A text editor with an open file dialog box, allowing the user to browse and select files from the system.

Explanation: A text editor with a print dialog box, allowing the user to configure printer settings and print the document.
Note: The above programs might not run in an online IDE please use an offline compiler.
For more methods on JSwing components refer: