Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

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

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/ComboBoxAutoComplete.java

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.

Bug is mentioned on https://bugs.openjdk.java.net/browse/JDK-8087549enter link description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...