membersound
membersound

Reputation: 86915

How to open a Vaadin ComboBox by code?

Is it possible to open a Vaadin ComboBox by code?

I'd like to present 2 comboboxes that depend on each other. When the user selects a value in the first, I'd like the 2nd combobox to automatically open the possible selections, so that the user can directly select one, instead of having to open the 2nd combobox himself.

Maybe there is an event that I could send to trigger the opening?

Upvotes: 2

Views: 1917

Answers (2)

Carlos Jaime C. De Leon
Carlos Jaime C. De Leon

Reputation: 2906

I've done this in Vaadin 7 with Selenium WebDriver:

public void selectValueInCombobox(WebElement cmb, String value) {
    cmb.findElement(By.tagName("div")).click(); //shows the menu
    List<WebElement> findElements = webDriver.findElements(By.cssSelector("td[role='listitem']"));
    findElements.stream()
        .filter(item-> value.equals(item.findElement(By.tagName("span")).getText()))
        .findFirst().get().click();
}

The WebElement is the Combobox (its the div when rendered in html), and value is the listitem in the combobox you want selected.

  1. It clicks the down arrow button on the rightmost (its an inner div)
  2. Which renders the listitem html elements, just find those with role is listitem, then via Java 8's stream, filter with the same value
  3. Once found, click and it will be selected

Upvotes: 3

nuzz
nuzz

Reputation: 647

The only thing I can think of is, after giving focus to the combobox, try sending it a keystroke (ie the down arrow to try and make it open). There is a good example here

The other way, maybe to try and use Selenium to trigger the combobox, but thats probably overkill. For examples of that, look into vaadin's testbench.

Upvotes: 2

Related Questions