How to disable the default context menu on a text field

前端 未结 3 472
死守一世寂寞
死守一世寂寞 2020-12-16 05:41

By default the JavaFX TextField has a built in ContextMenu with \'undo\', \'copy\', \'cut\' etc. options. The ComboBox also has the sa

3条回答
  •  天涯浪人
    2020-12-16 05:45

    As you have already stated, a call to GetContextMenu() returns null (which is the big clue the default one is an implementation detail) and if you add an additional ContextMenu it appears over the default one.

    Trying to replace the context menu with the following code:

    ContextMenu cm = new ContextMenu();
    cm.getItems().add(new MenuItem("Test"));
    
    textbox.setContextMenu(cm);
    

    Produces the following result.

    enter image description here

    Overriding the mouse clicked event isn't going to work either because you will still need to access the default Context menu via some property which doesn't seem to be possible.

    I've also checked the CSS reference to see if the ContextMenu was target-able via one of the controls sub-structures but again this returned no results.

    Based on this information it looks as though the default ContextMenu is an implementation detail of the TextField control (or maybe it's parent classTextInputControl) and can't currently be changed.

    Update

    I contacted Jonathan Giles (the tech lead at Oracle in the JavaFX UI controls team) who told me to file a bug report.

    I searched the bug database and have found some existing reports (RT-23213 & RT-24823) so it appears this is a known issue. As of today the issue is still open and is considered a medium priority but apparently it will be fixed for FX 8.0.

    From the bug report comments:

    The default context menu is created by the control's skin, which is currently not public API. We need to decide if and when the context menu should be accessible through the public API, but it will probably need to wait for the broader work to make skins more public.

提交回复
热议问题