When you right click on a TextField there are Undo, Redo, Cut, Copy, Paste, Delete, and Select All options.
I want to add a "Register" MenuItem to that list from my controller class, but do not know how.
Here is what I got so far:
This overwrites the existing menu items:
ContextMenu contextMenu = new ContextMenu();
MenuItem register = new MenuItem("Register");
contextMenu.getItems().add(register);
charName.setContextMenu(contextMenu);
Both of these return null:
charName.getContextMenu()
charName.contextMenuProperty().getValue()
You can replace the in-built TextField ContextMenu by setting your own (as below):
Adding to the in-built ContextMenu is a bit tricker and requires overriding non-public API.
You cannot get the in-built ContextMenu using the public
textField.getContextMenu
property as it is not returned (that method only returns a menu that has been set by the application code, not the internal JavaFX control skin implementation).Be aware that the following code will almost certainly break in Java 9 as it uses deprecated
com.sun
APIs which will likely no longer be available. For further details on this, refer to JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization