There are a lot of text editors available. There are those that run in the terminal, in a GUI, in a browser, and in a browser engine. Many are very good, and some are great. But sometimes, the most satisfying answer to any question is the one you build yourself.

Make no mistake: building a really good text editor is a lot harder than it may seem. But then again, it’s also not as hard as you might fear to build a basic one. In fact, most programming toolkits already have most of the text editor parts ready for you to use. The components around the text editing, such as a menu bar, file chooser dialogues, and so on, are easy to drop into place. As a result, a basic text editor is a surprisingly fun and elucidating, though intermediate, lesson in programming. You might find yourself eager to use a tool of your own construction, and the more you use it, the more you might be inspired to add to it, learning even more about the programming language you’re using.

To make this exercise realistic, it’s best to choose a language with a good GUI toolkit. There are many to choose from, including Qt, fltk, or GTK, but be sure to review the documentation first to ensure it has the features you expect. For this article, I use Java with its built-in Swing widget set. If you want to use a different language or a different tool set, this article can still be useful in giving you an idea of approaching the problem. Writing a text editor in any major toolkit is surprisingly similar regardless of what you choose.

If you’re new to Java and need further information on getting started, read my LINK-TO-NUMBER-GUESSING-GAME-ARTICLE-JAVA[Guessing Game] article first.

Project setup

Normally, I use and recommend an IDE like LINK-TO-NETBEANS-ARTICLE[Netbeans] or LINK-TO-ECLIPSE-ARTICLE[Eclipse], but I find that when practicing a new language it can be helpful to do some manual labour so you better understand the things that get hidden from you when using an IDE. In this article, I assume you’re programming using a text editor and a terminal.

Before getting started, create a project directory for yourself. In the project folder, create one directory called `src` to hold your source files.

----[source,bash] $ mkdir -p myTextEditor/src $ cd myTextEditor

Open the file in your favourite text editor (I mean your favourite one that you didn’t write) and get ready to code!

Package and imports

To ensure your Java application has a unique identifier, you must declare a *package* name. The typical format for this is to use a reverse domain name, which is particularly easy should you actually have a domain name. If you don’t you can use `local` as the top level. As usual for Java and many languages, the line is terminated with a semi-colon.

After naming your Java package, you must tell the Java compiler (`javac`) what libraries to use when building your code. In practice, this is something you usually add to as you code, because you rarely know yourself what libraries you need. However, there are some that are obvious beforehand. For instance, you know this text editor is based around the Swing GUI toolkit, so importing `javax.swing.JFrame` and `javax.swing.UIManager` and other related libraries are a given.

----[source,java] package com.example.textedit;

import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JTextArea; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.filechooser.FileSystemView; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger;

Amazingly, these few lines do about 80% of the work toward implementing a basic text editor, because JTextArea is Java’s text entry field. Most of the remaining 80 lines take care of helper features, like saving and opening files.

Building a menu

The `JMenuBar` widget is designed to sit at the top of a JFrame, providing as many entries as you want. Java isn’t a drag-and-drop programming language, though, so for every menu you add, you must also program a function. To keep this project manageable, I provide four functions: creating a new file, opening an existing file, saving text to a file, and closing the application.

The process of creating a menu is basically the same in most popular toolkits. First, you create the menubar itself, then you create a top-level menu (such as "File"), and then you create submenu items (such as "New", "Save", and so on).

----[source,java] public TextEdit() { run(); }

That’s *technically* all there is to this text editor. Of course, nothing’s ever truly done, and besides there’s still the testing and packaging steps, so there’s still plenty of time to discover missing requisites. In case you’re not picking up on the hint: there’s *definitely* something missing in this code. Do you know what it is yet? (It’s mentioned mainly in the LINK-TO-NUMBER-GUESSING-GAME-ARTICLE-JAVA[Guessing Game] article.)

Testing

You can now test your application. Launch your text editor from a terminal:

----[source,bash] $ java ./src/TextEdit.java error: can’t find main(String[]) method in class: com.example.textedit.TextEdit

You can try again, but now there are two files that depend upon one another to run, so you have to compile the code. Java uses the `javac` compiler, and you can set your destination directory with the `-d` option:

----[source,bash] $ javac src/*java -d .

Your text editor opens, and you can type into it, open files, and even save your work.

image: this-time-its-personal.png

Sharing your work as a Java package

While it seems to be acceptable to some programmers to deliver applications as an assortment of source files and hearty encouragement to learn how to run them, Java makes it really easy to package up your application so others can run it. You have most of the structure required, but you do need to add some metadata to a `Manifest.txt` file:

----[source,text] $ echo "Manifest-Version: 1.0" > Manifest.txt

From the syntax of the command, you may surmise that it creates a new JAR file called `TextEdit.jar`, with its required manifest data located in `Manifest.txt`. Its main class is defined as an extension of the package name, and the class itself is `com/example/textedit/Main.class`.

You can view the contents of the JAR file:

----[source,bash] $ jar tvf TextEdit.jar 0 Wed Nov 25 META-INF/ 105 Wed Nov 25 META-INF/MANIFEST.MF 338 Wed Nov 25 com/example/textedit/textedit/Main.class 4373 Wed Nov 25 com/example/textedit/textedit/TextEdit.class

You can even

create a .desktop file

so your application launches at the click of an icon in your applications menu.

Improve it

In its current state, this is a very basic text editor, best suited for quick notes or short README documents. Some improvements (such as adding a vertical scrollbar) is quick and easy with a little research, while others (such as implementing an extensive preferences system) require real work. But if you’ve been meaning to learn a new language, this could be the perfect practical project for your self-education. Creating a text editor, as you can see, isn’t overwhelming in terms of code, and it’s manageable in scope. If you use text editors frequently, then writing your own can be satisfying and fun. So open your favourite text editor (the one you wrote) and start adding features!

Proxied content from gemini://sdf.org/klaatu/geminifiles/java-text-edit.gmi (external content)

Gemini request details:

Original URL
gemini://sdf.org/klaatu/geminifiles/java-text-edit.gmi
Status code
Success
Meta
text/gemini
Proxied by
kineto

Be advised that no attempt was made to verify the remote SSL certificate.