How to prevent closing of AutoCompleteCombobox popupmenu on SPACE key press in JavaFX

后端 未结 2 1196
小蘑菇
小蘑菇 2020-12-21 02:35

I have created a AutoCompleteCombobox in JavaFX with the help of code mentioned on https://github.com/jesuino/javafx-combox-autocomplete/blob/master/src/main/java/org/fxapps

相关标签:
2条回答
  • 2020-12-21 03:05

    As a complement, this also works in java 8, you just have to import the internal Skin class:

    import com.sun.javafx.scene.control.skin.ComboBoxListViewSkin;
    
    0 讨论(0)
  • 2020-12-21 03:18

    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.

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

    I only tested this solution with Oracle Java 10 on Ubuntu, but it should work on other platforms as well.

    0 讨论(0)
提交回复
热议问题