Calling Javascript Function

I’ve read the documents at https://vaadin.com/wiki/-/wiki/Main/Vaadin+7 but I am still having problems getting a simple test case running.

I want to call a javascript function after a vaadin button is pressed. For simplicity, i have a bar.js that looks like


window.com_foo_bar_Bar = function() {
    
    alert("Here");
    
    function test() {
    	alert("test called");
    }
}

I then created a java file Bar.java:


@JavaScript({ "bar.js"})
public class Bar extends AbstractJavaScriptExtension  {
	private static final Logger log = Logger.getLogger(Bar.class);
	
	@Override
        protected Class<? extends ClientConnector> getSupportedParentType() {
              return UI.class;
        }
  	
  	public void testIt() {
  		log.info("DownloadFile");
  		 callFunction("test");
  	}
}

The javascript file and the java class are in the same package.

Now to run it, on a button handler I have:


private void onClick() {
		log.info("Button Clicked");
		 Bar bar= new Bar();
		 bar.testit();		
	}

I would expect after clicking my button to get an alert from the javascript, but I see the log messages, but no alerts.

I am sure I am doing something stupid. Can you point me in the right direction? All the examples I see online have the javascript returning a UI component.

Thanks

I got it to work when I call extend(UI).

can you give me a code snippet of what you correct with that made it work?

hi dustin hunter,

i want to call a js funtion on button click but i can’t do.

how did you do it, could you explain please? i need to your suggestion.

thanks a lot.

just with java no script :slight_smile:

Robot robot = Robot();
robot.keyPress(KeyEvent.VK_F11);
robot.keyRelease(KeyEvent.VK_F11);