How To Create A Text Editor

How To Create A Text Editor


It is a well known fact that the best way to practice programming is through practice. Short coding exercises/challenges are helpful when practicing a specific concept, for example, how to use loops correctly. However, these challenges are not always applicable to real-time systems and they do little to help one understand how to build a fully functioning project.

In my last post, I demonstrated how to create a game of Hangman, with all the source code contained in a single .java file. Today, I will focus on another project that is great for practicing programming; creating a text editor.

text editor

A text editor is one of the best projects you can build. One like notepad can be written in an hour, but something more complex like sublime text is a life project. For this post, we will be creating a simple editor with basic functionality. You can continue to work on the project in your own time and possibly build a more complex editor which can someday become a popular download much like sublime text.

Step 1: The Textarea

  • JTextArea textArea:Creates a multi line region that displays text. It allows the editing of multiple line text. It displays no text initially.
  • JScrollPane areaScrollPane:Used to make scroll-able view of a component. When screen size is limited, we use a scroll pane to display a large component or a component whose size can change dynamically.
  • JMenuBar menuBar:Used to display menubar on the window or frame. It may have several menus.

Step 2: The Menu

The JMenu and JMenuItem is used to create a pull down menu component which is displayed from the menu bar. In this example, there are 3 menu items; File, Edit and Help. The image below shows the JMenu and JMenuItem components.

menu and menuitem

Step 3: The Constructor

There are two main parts to the constructor. The first is shown above and is responsible for setting the default display settings of the main components; the JTextArea and the JScrollPane. As you can see the size, font and title are set here.

When both are set, they are then added to the ContentPane which is the layer or container that is used to hold objects in Java Swing. The JMenuItems are also added to the JMenu component from here.

The last part of the constructor sets the shortcuts for each of the MenuItems specified. In the snippet above, the ‘Open’ MenuItem can be activated by using the ‘Ctrl + O’ Option.

Step 4: Action Events

The final method is the actionPerformed which is invoked automatically whenever you click on the registered component. It specifies what the MenuItem should do. In the snippet, the ‘Open’ menu item must open the FileChooser to allow the user to select a file.

Step 5: The Main Method

Source Code

Below is the full source code for this project.

The final image when it is run is shown below.

final notepad

What other features can you add to this project? Try to add features like syntax highlighting, code completion, find, replace etc. If you wish, add it on GitHub and make it an open source project so that other programmers can add their ideas. You could be on the way to making the next best text editor!

Seda
Seda Kunda is a web designer and developer with a degree in Computer Science and a great passion for code. Besides code, she enjoys pepperoni pizza, watching the bachelor and sleeping in on Saturdays.
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

4 thoughts on “How To Create A Text Editor

    1. Hi Trevor, there’s a number of approaches and languages that can be used to create a text editor. This is just one approach and I chose it because it is easy to understand. Feel free to add features to this one or create a completely new one with a different approach and share your work. I try not to do multiple posts of the same thing.

Comments are closed.