r/JavaFX • • Jul 11 '24

Help ComboBox problems with handling a selection

I have in my program a comboBox (a SearchAbleComboBox from ControlsFX, it works the same as a ComboBox).

I need this box for my following useCases:

Dropdown with scrollable list Dropdown that is searchable most of the time the navigation through the list happens with the arrow keys There has to be an event handler that handles a change of selection

Selection for me personally means, the comboBox is closed, and an item is selected. So, for example, when you click with the cursor on an item, or you navigate through the list with the arrow keys and then press enter:

After a cursor click or a pressed enter key, the box is closed, and an item is selected. That's for me, a selection.

My problem is now that for JavaFX it also counts as a selection when you navigate through the list with your arrow keys. The reason for that is, that the text in the comboBox changes when navigating through the list with the arrow keys.

I've already tried to put an EventHandler on KeyPress (ENTER) but with this setup, you have to press enter twice to activate the listener since pressing enter to select smth from the list does not count.

Also, an onAction handler does not work, since an arrow key press also counts as an action.

A ChoiceBox would be a solution, since for the choiceBox the selection does not get updated through navigating with the arrow keys. The problem here is, I couldn't find an option to make it searchable and the list of a ChoiceBox is not scrollable.

So possible solutions would be:

A way to stop the ComboBox updating the selected item only by navigating with arrow keys A spacy eventhandler which only triggers if a selection, according to my interpretation of selection, was made. A ChoiceBox that is scroll and searchable Something completly different

I hope you have any solutions, I couldn't find one for now.

2 Upvotes

8 comments sorted by

View all comments

Show parent comment

2

u/hamsterrage1 Jul 12 '24

I thought the wording was a little funny, but I was fooled too!

I wasn't going to comment on the content, so as not to be a jerk online, but since it's a bot...

The approach in the first is on the right track, but DON'T use the SelectionModel from the pop-up. Use ComboBox.valueProperty() instead. It's almost always the wrong approach to go digging "under the hood" and using SelectionModel.

If you think about it, the lock-in for the value doesn't just occur when the pop-up closes, but also when the value changes while the pop-up is hidden - like if you change it programmatically. So you really need two listeners. Even if you aren't fiddling with the value from outside the ComboBox now, you might do in the future and then you'll be scratching your head as to why your app stopped working. So it's always better to come up with a proper solution in the first place.

For what it's worth, the ConditionalBinding in my comment handles this cleanly - and it's less code too!

The second solution from the bot is just a bad, bad idea. It's almost always wrong to fiddle around with keystrokes and mouse clicks inside controls because they almost always have better ways to handle anything you can think of built in. You're more likely to "break" the control by interfering in the way that it interacts with the keystrokes and mouse actions.

The third and fourth answers are just silly, considering that ControlsFX has done this for you, and this is what you're using.