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.
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.
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
Below is the full source code for this project.
The final image when it is run is shown below.
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!