The first time I encountered this I really did not pay much attention but after experiencing it the second time in my other ComboBox, I decided to look into it.

The issue happens when I click on the ComboBox the first time and the popup menu list that is shown has its width very short. The second time I click on the ComboBox and the list is shown again, the width is now correct as the width of the list now aligns itself with the ListView.

combobox_popup_width_small

Seems this is a bug in Java FX 2. User shakir.gusaroff in the Oracle forumprovided an insightful answer regarding this issue.

There is a bug: https://javafx-jira.kenai.com/browse/RT-28876 where the ComboBox popup list is set with the wrong width the first time it is shown if it is wider than prefWidth.

This is fixed in Java FX 8.0 already, but not in Java FX 2.

Well, since there is no built-in class that does this functionality, the only solution was to create my own. Here is my version of an auto-complete class that can be used for a TextField or editable ComboBox.

If you want to use this for a TextField node, just change parts of the code to cater to it. It should not be hard.

To use this class, just do

Easy, right?

This class also includes an added feature wherein after you type something in the editable ComboBox and when you press the ENTER key, an action will take place: say, you want to save it to the database or something.

Note: the focus listener is now added to the ComboBox’s TextField component because in the development environment, it works fine. However, when deployed as an Applet, it does not work. Only when I shifted the focus listener to the ComboBox’s editor did it work correctly.

Well, I bet you coded it the obvious way by using the setOnKeyPresse9) or setOnKeyReleased() method of the ComboBox class. For explanations that I still do not understand and do not care as long as it worked, user James D from the Oracle forum stated that the space, enter and escape key do not register because the button itself of the ComboBox captures that event (and propagates it as an action event), thus consuming it before your event handler.

The solution to this is not to use addEventHandler() but instead use addEventFilter().

This is how to call it:

You should now be able to capture the escape, enter and space key events.

Related Posts Plugin for WordPress, Blogger...