Skip to content

Example: Adding an Edit menu for the editor

bobbylight edited this page Sep 5, 2021 · 2 revisions

If you're building an application with a code editor, it's common to have an "Edit" menu with actions such as Undo, Redo, Cut, Copy, Paste, etc. RSyntaxTextArea makes it easy to get the actions used by the editor so you don't have to implement them yourself.

The base class RTextArea has constants defined for all of the above standard actions:

  • RTextArea.CUT_ACTION
  • RTextArea.COPY_ACTION
  • RTextArea.PASTE_ACTION
  • RTextArea.DELETE_ACTION
  • RTextArea.UNDO_ACTION
  • RTextArea.REDO_ACTION
  • RTextArea.SELECT_ALL_ACTION

These can be passed to the getAction(int) method to retrieve a RecordableTextAction, which is really just a Swing Action with macro support and a bunch of utility methods for the editing the basic Action properties. You can then use these actions to create your JMenuItems for your "Edit" menu.

Also note that the action names and mnemonics are already localized into many languages. If you see any errors, or want to contribute to the localization of RSyntaxTextArea, feel free to send me an email or simply open a pull request with the changes.

Here's a simple example:

import java.awt.*;
import javax.swing.*;
import org.fife.ui.rtextarea.*;
import org.fife.ui.rsyntaxtextarea.*;

/**
 * A simple example showing how create an "Edit" menu from the same
 * actions used by RSyntaxTextArea.<p>
 *
 * This example uses RSyntaxTextArea 3.0.5.
 */
public class EditMenuDemo extends JFrame {

    public EditMenuDemo() {

        JPanel cp = new JPanel(new BorderLayout());

        RSyntaxTextArea textArea = new RSyntaxTextArea(20, 60);
        textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
        textArea.setCodeFoldingEnabled(true);
        RTextScrollPane sp = new RTextScrollPane(textArea);
        cp.add(sp);

        setJMenuBar(createMenuBar(textArea));

        setContentPane(cp);
        setTitle("Edit Menu Demo");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);

    }

    private static JMenuBar createMenuBar(RSyntaxTextArea textArea) {

        JMenuBar menuBar = new JMenuBar();

        JMenu editMenu = new JMenu("Edit");
        editMenu.add(createMenuItem(RTextArea.getAction(RTextArea.UNDO_ACTION)));
        editMenu.add(createMenuItem(RTextArea.getAction(RTextArea.REDO_ACTION)));
        editMenu.addSeparator();
        editMenu.add(createMenuItem(RTextArea.getAction(RTextArea.CUT_ACTION)));
        editMenu.add(createMenuItem(RTextArea.getAction(RTextArea.COPY_ACTION)));
        editMenu.add(createMenuItem(RTextArea.getAction(RTextArea.PASTE_ACTION)));
        editMenu.add(createMenuItem(RTextArea.getAction(RTextArea.DELETE_ACTION)));
        editMenu.addSeparator();
        editMenu.add(createMenuItem(RTextArea.getAction(RTextArea.SELECT_ALL_ACTION)));
        menuBar.add(editMenu);

        return menuBar;
    }

    private static JMenuItem createMenuItem(Action action) {
        JMenuItem item = new JMenuItem(action);
        item.setToolTipText(null); // Swing annoyingly adds tool tip text to the menu item
        return item;
    }

    public static void main(String[] args) {
        // Start all Swing applications on the EDT.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new EditMenuDemo().setVisible(true);
            }
        });
    }

}