Java - How to hook into the Copy and Paste menu on the Mac OS

I understand that you can:

// Where CustomMacOSXController implements implements MRJAboutHandler, ...
CustomMacOSXController macOSXController = new CustomMacOSXController()

MRJApplicationUtils.registerAboutHandler(macOSXController); 
MRJApplicationUtils.registerPrefsHandler(macOSXController);
//... and so on

However I can't find anything to hook the copy and paste menu so that when it's called, the currently selected JTextField will be affected (for example the paste will paste the clipboard into the selected JTextField.


Solution 1:

Unlike the Mac OS X System and Application menu, the Edit menu is entirely under the purview of your program. You have to create and populate it with the approariate Action. The pre-defined subclasses defined in javax.swing.text.TextAction are handy, as they are aware of the focused component. See also this related Q&A and example. For example,

Action pasteAction = new DefaultEditorKit.PasteAction();
JMenuItem pasteItem = new JMenuItem(pasteAction);
JButton pasteButton = new JButton(pasteAction);

Addendum: To fully integrate your Edit menu into Mac OS X, you must tell the operating system to use your menu, using one of the approaches shown here. To obtain the platform-dependent modifier key, use getMenuShortcutKeyMask(), as shown here. Finally, Charles Bell's HTMLDocumentEditor is an example that uses the text actions.