Jcombobox selecting an element

74 Views Asked by At

I've got a JComboBox that replicates a dropdownmenu from a website. I retrieve the options via HtmlUnit. When the data is retrieved the jcombobox looks like this:

jcombobox1

Now obviously I want to select another year (let's say 2015) and retrieve the data that comes with that year and when I do this the following job gets executed:

         int option = jComboBox_year.getSelectedIndex(); 
//Gets the index of the selected combobox option

         HtmlSelect year = (HtmlSelect) page2.getElementById("dip_selector_select_i00083_5"); 
//this selects the dropdownmenu from the webpage

 HtmlOption choice = year.getOption(index);
//gets the option with the right index from the dropdownmenu

         year.setSelectedAttribute(choice, true);
//sets the selected attribute at the website

After this code my program waits for the resulting page to load and then starts collecting data from the site based on the changed option.

After my described code however the output combobox looks like this:

jcombobox2

After this it still retrieves the data that goes with year 2016. So basically the option 2015 gets selected in some kind of way, but not in the right way. Can anyone help??

Thanks in advance!

1

There are 1 best solutions below

1
On

HtmlSelect.setSelectedAttribute changes the value of the selected attribute of the given option. But usually you like to change the selection of the select box and not the dom attribute. Try using HtmlOption#setSelected(true). This will make the option the selected one (including deselecting the currently selected one).

In your code do choice.setSelected(true).

Hope that helps...