tags:

views:

42

answers:

1
+1  Q: 

GWT : print button

Hi, I am trying to create a button that prints the current browser window.

This is my current code, that uses (or at least it tries to use) JSNI:

private Button print = new Button(constants.print(), new ClickHandler() {
    @Override
    public void onClick(final ClickEvent event) {
        /*-{
            if ($wnd.print) { 
                $wnd.print(); 
                return true; 
            } else { 
                return false; 
            } 
        }-*/
    }           
});

But when I click the button, nothing happens. It is my first GWT application, so I am not sure about how to implement it.

+4  A: 
new Button(constants.print(),  new ClickHandler() {
        @Override
        public void onClick(final ClickEvent event) {
           print();
        }

        private native boolean print( ) /*-{
            if ($wnd.print) { 
                 $wnd.print(); 
                 return true; 
            } else { 
                 return false; 
            } 
        }-*/;  });

Should work! Always place JSNI within a native method.

Ashwin Prabhu
Thank you very much. It works.
Guido
By the way, I've found you can also use the static method Window.print() provided by GWT and avoid using JSNI.
Guido