如何防止在 JavaFX 中按下空格键时关闭 AutoCompleteCombobox 弹出菜单 [英] How to prevent closing of AutoCompleteCombobox popupmenu on SPACE key press in JavaFX
问题描述
但问题是组合框弹出窗口在用户按下空格键时关闭.我想继续用空格字符过滤并防止弹出窗口关闭.
But issue is that combobox popup closes when user presses SPACE key. I want to continue filtering with space character and prevent popup from closing.
我已经处理了组合框上的所有三个事件(按键、释放、键入),但没有解决方案.我认为这是由组合框项目列表视图上的按键事件引起的.
I have handled all three events (key press, key release, key typed) on combobox but no solutions. I think it is being caused by key press event on combobox item list view.
在 https://bugs.openjdk.java.net/browse 上提到了错误/JDK-8087549在此处输入链接描述
我只想知道如何覆盖处理空格键按下的事件处理程序.
I just want to know how can I override the event handler which handles SPACE key press.
推荐答案
我也一直在尝试创建一个 AutoCompleteCombobox 并且想知道为什么每次进入空间时弹出窗口都会关闭,直到我得到你的提示,实际错误位于 ComboBoxListViewSkin 类中.
I have been trying to create a AutoCompleteCombobox as well and was wondering why the popup gets closed every time you enter space, until I got your hint that the actual bug is in the ComboBoxListViewSkin class.
您只需要将 ComboBox 的皮肤替换为一个带有 EventFilter 的新皮肤.
You just need to replace the skin of the ComboBox with a new one, which has an EventFilter.
ComboBoxListViewSkin<T> comboBoxListViewSkin = new ComboBoxListViewSkin<T>(comboBox);
comboBoxListViewSkin.getPopupContent().addEventFilter(KeyEvent.ANY, (event) -> {
if( event.getCode() == KeyCode.SPACE ) {
event.consume();
}
});
comboBox.setSkin(comboBoxListViewSkin);
我仅在 Ubuntu 上使用 Oracle Java 10 测试了此解决方案,但它也应该适用于其他平台.
I only tested this solution with Oracle Java 10 on Ubuntu, but it should work on other platforms as well.
这篇关于如何防止在 JavaFX 中按下空格键时关闭 AutoCompleteCombobox 弹出菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!