r/JavaFX • u/PalBeron • 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.
View all comments
Show parent comment
1
u/hamsterrage1 Jul 13 '24
I did some further testing, changing the SearchableComboBox to a ComboBox. There is one key difference between the two controls, as far as this issues is concerned...
With ComboBox, if you just arrow down when the control has focus, it will start to cycle through the values without opening up the pop-up. The valueProperty() changes, and hitting <Esc> won't abandon the edit and put it back. This means that whatever shows in the ComboBox is the "selected" value as defined by the OP.
With SearchableComboBox, when you hit the down arrow, the main textbox changes to the filter mode and the pop-up opens up. I checked the source code and this is explicitly coded to behave this way, although it seems to me to be such a key difference from ComboBox that you could almost consider it a bug.
In any event, you cannot cycle through the values in SearchableComboBox without having the pop-up open, which means that the solution provided by the bot has a better chance of working.
However, just looking at the bot's code, it still shouldn't work. Because it's only locking in the selection when the selection changes while the pop-up is hidden. But when the pop-up closes, the selection isn't changing - it's just closing the pop-up. So the Listener should NEVER fire when the pop-up is closed because you cannot change the selection with the pop-up closed!
When I tested it, SearchableComboBox cycles both the valueProperty() and the selectedItem() property through null and then back to the last value again. The first change is with the pop-up showing, and the second happens after the pop-up has closed.
However, when I tested it with ComboBox, this doesn't happen. When the pop-up is closed, no changes are made to either SelectionModel.selectedItem() or valueProperty().
So, the code provided by the bot simply cannot work for ComboBox, but will work for the specific case of SearchableComboBox in ControlsFX because of the slightly bizarre behaviour of SearchableComboBox.