如何防止在JavaFX中的SPACE键按下关闭AutoCompleteCombobox弹出菜单 [英] How to prevent closing of AutoCompleteCombobox popupmenu on SPACE key press in JavaFX

查看:124
本文介绍了如何防止在JavaFX中的SPACE键按下关闭AutoCompleteCombobox弹出菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 https://github.com/jesuino/javafx-combox-autocomplete/blob/master/src/main/java/org/fxapps/ComboBoxAutoComplete.java

但问题是当用户按下SPACE键时,组合框弹出窗口关闭。我想继续使用空格字符过滤并阻止弹出窗口关闭。

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 在这里输入链接描述

我只想知道怎么样我重写了处理SPACE键的事件处理程序。

I just want to know how can I override the event handler which handles SPACE key press.

推荐答案

我一直在尝试创建一个AutoCompleteCombobox并且想知道为什么每次输入空格时弹出窗口都会关闭,直到我得到提示,实际的bug在 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中的SPACE键按下关闭AutoCompleteCombobox弹出菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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