VOOZH about

URL: https://www.geeksforgeeks.org/java/java-swing-create-a-simple-text-editor/

⇱ Java Swing | Create a simple text editor - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Swing | Create a simple text editor

Last Updated : 1 May, 2026

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

methodexplanation
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:

Output:

👁 Image

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

👁 Image

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

👁 Image

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

👁 Image

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:

Comment
Article Tags: