Leaf - Cleaning Up an Old Java Swing Text Editor

5 min read

Leaf is a lightweight desktop text editor built with Java Swing. It can open and save local files, search and replace text, insert formatted date and time values, track line and column position, browse files from a side tree, save font preferences, and switch into a simple Java highlighting mode.

I originally made Leaf years ago while learning Java, then left it sitting outside GitHub for a long time. Bringing it back was less about turning it into a serious editor and more about cleaning up an old learning project enough that it could build, run, and be shared publicly.

Why I built it

At the time, I wanted to understand what a real desktop application felt like under the hood. A text editor was a good project because it touches a lot of practical Java topics at once: Swing layouts, menu actions, file I/O, event listeners, text documents, keyboard shortcuts, user preferences, and cross-platform behavior.

It also forced me to think beyond “put a button on the screen.” The app needed to remember whether a document had unsaved changes, prompt before replacing work, keep the window title updated, handle Save versus Save As, and keep the interface responsive while browsing files.

Leaf is not a production-ready editor. It is an old project that has been cleaned up and documented so it can stand on its own as a learning project.

Features

The core editing flow is local and simple. You can create a new untitled document, open an existing file, save in place, or save a new file through Save As. If there are unsaved changes, Leaf prompts before opening another file, creating a new document, or exiting.

The editor also includes undo and redo through Swing’s UndoManager, cut/copy/paste through the system clipboard, a Find / Replace dialog, and date/time insertion in short or long formats.

For navigation and status, Leaf has an optional file manager tree that can browse from an opened path, a status bar that tracks the current line and column, and selected character counts. It also supports a start/end selection mode and a column selection mode for rectangular text selections across multiple lines.

Java highlighting

Leaf includes a simple Java mode that highlights common Java keywords while editing. It uses a styled Swing document instead of a full parser, so the feature is intentionally basic. The goal was to learn how styled text components work, not to recreate a full language server or IDE.

That limitation is important. Keyword highlighting is useful for demonstrating document styling, but Leaf does not try to provide deep code intelligence, autocomplete, diagnostics, or project-wide Java analysis.

Settings

One part I wanted to handle properly was font preferences. Leaf lets you change the editor font family, style, and size, then stores those preferences in the user’s normal config location for the operating system:

  • Windows: %APPDATA%\Leaf
  • macOS: ~/Library/Application Support/Leaf
  • Linux: $XDG_CONFIG_HOME/leaf or ~/.config/leaf

That kept user settings outside the source tree and made the app behave more like a normal desktop program.

Structure

The app is organized around a Swing JFrame containing the main JTextPane, menu bar, optional file tree, and status bar.

  • editor.Main starts the application and exposes small self-test and startup-test modes.
  • editor.TextEdit builds the main window, menus, editor pane, file handling, status updates, font dialog, and formatting behavior.
  • editor.Find provides the Find / Replace window and search logic.
  • editor.Settings loads and saves font preferences in the correct user config directory.
  • editor.ColumnSelectCaret implements rectangular selection behavior.
  • converter.TimeFormat, converter.DayConverter, and converter.MonthConverter format inserted date and time values.
  • treeNodes.FileNode and treeNodes.CreateChildNodes build the file manager tree.

Building and running

Leaf requires Java 22 or newer. The release build is packaged as a runnable JAR, available here, so the easiest way to try it is:

run.shShell
java -jar leaf.jar

From source, it can be compiled with javac:

run-2.shShell
javac --release 22 -d out $(find src -name "*.java")
java -cp out editor.Main

The repository also includes smoke checks for the non-visual parts of the application, plus a startup test for confirming that the GUI startup path constructs successfully from a desktop session.

Source code

The source code and releases are available on GitHub:

github.com/connorcarro/leaf

The latest release includes a leaf.jar artifact that can be launched with Java 22 or newer.

Conclusion

Leaf is not the kind of project I would design the same way today, but that is part of why I like it. It shows an earlier stage of learning Java and desktop programming, and cleaning it up gave me a chance to make the project buildable, testable, and understandable without pretending it is more advanced than it is.

Building it taught me how much work even a “simple” editor needs: document state, menus, preferences, file handling, selection behavior, and small UX decisions all pile up quickly. It was a practical way to learn Swing and a useful reminder that desktop applications have a lot of moving parts even before they become polished products.

Back to top