如何禁用文本字段上的默认上下文菜单 [英] How to disable the default context menu on a text field

查看:123
本文介绍了如何禁用文本字段上的默认上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,JavaFX TextField具有内置的ContextMenu,具有撤消",复制",剪切"等选项.当ComboBox设置为可编辑时,它也具有相同的ContextMenu(ComboBox实际上是编辑器的一部分,它是TextField).

By default the JavaFX TextField has a built in ContextMenu with 'undo', 'copy', 'cut' etc. options. The ComboBox also has the same ContextMenu when it is set as editable (the ComboBox is actually part of the editor which is a TextField).

我想用自定义项替换此ContextMenu,但是禁用默认项时遇到问题.

I want to replace this ContextMenu with a custom one but I'm having a problem with disabling the default one.

我尝试使用ContextMenu和鼠标单击事件,但是ComboBoxComboBox.getEditor()都具有空的ContextMenu.

I have tried consuming the ContextMenu and mouse click events but ComboBox and ComboBox.getEditor() both have a null ContextMenu.

我想念什么吗?

推荐答案

如前所述,对GetContextMenu()的调用返回null(这是一个大提示,默认为实现细节),并且如果添加了另外的ContextMenu会出现在默认的ContextMenu上.

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);

产生以下结果.

覆盖鼠标单击事件也不起作用,因为您仍然需要通过似乎不可能的某些属性来访问默认的上下文"菜单.

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.

我还检查了CSS参考,以查看ContextMenu是否可以通过控件子结构之一定位到目标,但同样没有返回结果.

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.

基于此信息,似乎默认的ContextMenuTextField控件的实现细节(或者它可能是父类TextInputControl),并且当前无法更改.

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.

更新

我联系了Jonathan Giles(JavaFX UI控件团队Oracle的技术负责人),他告诉我提交错误报告.

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

我搜索了错误数据库,并找到了一些现有的报告(RT-23213和RT-24823),因此看来这是一个已知问题.到今天为止,该问题仍然悬而未决,被认为是中等优先级,但显然将在FX 8.0中解决.

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.

来自错误报告的注释:

默认上下文菜单由控件的外观创建,即 目前不是公共API.我们需要确定上下文是否以及何时 菜单应该可以通过公共API访问,但可能 需要等待更广泛的工作以使皮肤更加公开.

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.

这篇关于如何禁用文本字段上的默认上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆