
For this article, I use Java with its built-in Swing widget set. 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.

To make this exercise realistic, it’s best to choose a language with a good GUI toolkit. 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. As a result, a basic text editor is a surprisingly fun and elucidating, though intermediate, lesson in programming. The components around the text editing, such as a menu bar, file chooser dialogues, and so on, are easy to drop into place. In fact, most programming toolkits already have most of the text editor parts ready for you to use. But then again, it’s also not as hard as you might fear to build a basic one. Make no mistake: building a really good text editor is a lot harder than it may seem. The listener's constructor takes an argument specifying an undo or redo. One ActionListener is used for both buttons. There are buttons for each of undo and redo actions. The listener's undoableEditHappened() method has the following code: undoMgr_.addEdit(e.getEdit()) // e is the UndoableEditEvent and undoMgr_ is the UndoManager The UndoManager manages the list of UndoableEdits, providing a way to undo or redo the appropriate edits. The operations are added to a UndoManager. The UndoableEditListener is added to the StyledDocument this listener hears the undoable operations occurring in the document. The edited text can be undone or redone using the undo and redo functions. tForeground(attr, newColor) Įditor_.setCharacterAttributes(attr, false) SimpleAttributeSet attr = new SimpleAttributeSet()
#Java text editor code#
The following snippet shows the code from the set color button's ActionListener.actionPerformed() method: Color newColor = JColorChooser.showDialog(.) This color is applied to the selected text. The set color button when pressed opens a JColorChooser dialog where a color can be selected. Note that the bold, italic and underline buttons act to toggle their respective attribute on the selected text. This brings the cursor back to the editor where it was previously before the cut button was pressed. The listener's actionPerformed() method has a single statement: editor_.requestFocusInWindow(). After some text is selected in the editor and a button (for example, the cut button) is pressed, the cursor does not return to the editor. The EditButtonActionListener is an ActionListener attached to all the six buttons. The following code snippet shows how the cut action button is defined: JButton cutButton = new JButton(new CutAction()) ĬutButton.addActionListener(editButtonActionListener) The bold, italic and underline format actions are defined in the StyledEditorKit.

These actions are defined in the DefaultEditorKit. The cut, copy and paste edit functions are created from Actions supplied to the JButtons. Cut, copy, paste, bold, italic and underline In the finished application type in some text edit text using cut (CTRL-X), copy (CTRL-C) and paste (CTRL-V).ģ.1. JScrollPane editorScrollPane = new JScrollPane(editor_) Įditor_.setDocument(new DefaultStyledDocument()) įrame_.add(editorScrollPane, BorderLayout.CENTER) The following code snippet shows the statements used to build the GUI: frame_ = new JFrame(MAIN_TITLE) The JTextPane is associated with a DefaultStyledDocument this is for editing the styled text (like bold, italic, various fonts and sizes, colors, etc.). The GUI look and feel is set as Nimbus and the default font for the text editor is set as SansSerif with 18 point size.
#Java text editor download#
The Java source code for the finished application can be downloaded from the Download section below.
