PeachesToad PeachesToad - 3 months ago 52
Java Question

Why is Vaadin ClickListener not firing on first click?

I have a table with several rows. When user click on a row the

ItemClickListener
enables a button:

testCaseTable.addItemClickListener(new ItemClickListener() {

@Override
public void itemClick(ItemClickEvent event) {

groupForm.tcDetailsButton.setEnabled(true);

System.out.println("_________________");
System.out.println("Item ID is: " + event.getItemId().toString());
System.out.println("Item is: " + event.getItem().toString());
System.out.println("Property ID is: " + event.getPropertyId().toString());
System.out.println("_________________");
}
});


The first time I click the button, nothing happens. The second time I click the button, it fires properly and opens the browser window.

tcDetailsButton.addClickListener(new Button.ClickListener() {

/**
*
*/
private static final long serialVersionUID = 3726094749730769327L;

@Override
public void buttonClick(ClickEvent event) {
BrowserWindowOpener opener = new BrowserWindowOpener(EvoltwebUI.class);
opener.setResource(new ExternalResource("google.com")); //url
opener.setWindowName("_blank");
opener.extend(tcDetailsButton);

}
});


It appears that if I click on 2 rows in my table, then click on the button, it opens 1 browser tab. If I click on 3 rows, then click on the button, it opens 2 browser tabs, 4 rows clicked = 3 tabs opened, etc.

What could be causing this?

Answer

I was able to fix this by following Uladzimir Kordzik's suggestion above, but instead, I moved the declaration and instantiation of BrowserWindowOpener to the class level and extend the component to the opener before the click listener:

Class level declaration and instatiation:

public BrowserWindowOpener opener = new BrowserWindowOpener(EvoltwebUI.class);

Extend opener before click:

opener.extend(tcDetailsButton);

Click listener looks like this now:

tcDetailsButton.addClickListener(new Button.ClickListener() {

        /**
         * 
         */
        private static final long serialVersionUID = 3726094749730769327L;

        @Override
        public void buttonClick(ClickEvent event) {             

            opener.setResource(new ExternalResource("google.com")); //url
            opener.setWindowName("_blank");                         

        }
});