2

I have a Source ComboBox to populate source fields (25-30 items) shown below in first page

"A"

"B"

...

"Z"

I have selected last item from ComboBox as shown below

"Z"

and when traversing to the next page after saving, i need to make the source combo selection blank, so i have return the below code to reset the Source combobox to point to first item (to reset the display to start from top of dropdown list for user selection)

// my first value in source List is empty space - “”

                    sourceComboBox.setValue("");

even if you use below code snippets like

                    sourceComboBox.getSelectionModel().selectFirst(); 
                    sourceComboBox.getItems().clear();
                    sourceComboBox.getSelectionModel().clearAndSelect(0);

but when i click open the combobox dropdown it still shows dropdown display from bottom as shown below

...

"X"

"Y"

"Z"

I am unable to post images for representing combobox values, so has put in above examples.

This looks like a graphics bug to me or am I doing something wrong? I have seen similar issue reported in below question but no work around suggested so far Combobox clearing value issue

Community
  • 1
  • 1
mathew
  • 21
  • 1
  • 2

5 Answers5

1

If you want to simply "reset" the combo box, I think all you have to do is set the value to null, like so:

sourceComboBox.setValue(null);
RonSiven
  • 939
  • 2
  • 10
  • 23
0

I ran into nearly the exact same situation and came across your question while looking for a solution. Fortunately, I came up with a workaround that forces the ComboBoxes to reset. When you reset the data on your pane, instead of doing something like:

sourceComboBox.getSelectionModel().clearSelection();
sourceComboBox.getItems.clear();

do something like this...

parentNode.getChildren().remove(sourceComboBox);
sourceComboBox= new ComboBox();  // do whatever else you need to format your ComboBox
parentNode.add(sourceComboBox);

You'll also need to do a setItems() again on your ComboBox so the new one will be populated. This is not an ideal solution but it seems to be working as I expect the provided clearSelection() method would.

Brendan
  • 3,589
  • 3
  • 20
  • 11
0

Below is the code you're looking for. It will reset it to whatever index you give in within the parenthesis.

sourceComboBox.setSelectedIndex(0);

Goodluck

0

Most of the things were giving me an error when I tried them. What worked best for me was to use this

comboBox.getSelectionModel.clearSelection();

This will essentially set the value of your ComboBox to null. Because of this, if you are referring to the value of the ComboBox in another place, it becomes important to check for whether the value of the ComboBox is null using this

if(comboBox.getValue() != null)
  //Code segment here
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
-1

The ComboBox control has not a method like scrollTo(index) of the ListView. So it seems hard to workaround that behavior. You can issue a feature request at JavaFX Jira.

Uluk Biy
  • 48,655
  • 13
  • 146
  • 153