Search code examples
gwtjsni

GWT : print button


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.


Solution

  • 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.