Adding a Button node inside a ListView in Java FX 2 is pretty easy. Create a custom class that extends ListCell and integrate your object within it. See my sample class code.

Then use the class in the ListView object by doing it like this:

That’s it! Easy, right?

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...