Thomas
Thomas

Reputation: 189

GWT-Button acting as hyperlink

I am trying to create an "Open" button which will open a new website.

Unfortunately, I don't understand how to create an event that opens a new website. How can I register the Hyperlink widget to this ClickEvent :

 Button button = new Button("Open");
 button.addClickHandler(new ClickHandler() {
  public void onClick(ClickEvent event) {  
          Hyperlink widget = new Hyperlink("Home Page", "Home");

             }
 });
 rootPanel.add(button, 568, 275);

thanks in advance !

Upvotes: 3

Views: 10203

Answers (3)

user467871
user467871

Reputation:

You can't do it with hyperlink. You should do it with anchor or window assign or just simple native javascript methods like that

Button button = new Button("Open");
button.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {  
          Window.Location.assign("new url");

or

          getURL("new url");

or add anchor to the panel

         Anchor a = new Anchor("new page", "new url");
         RootPanel.get().add(a);
     }
});
rootPanel.add(button, 568, 275);


public static native String getURL(String url)/*-{
        return $wnd.open(url, 'target=_blank')
    }-*/;

Upvotes: 2

Jason Hall
Jason Hall

Reputation: 20930

This code will open the link in the current window when the button is clicked:

Button button = new Button("Open");
button.addClickHandler(new ClickHandler() {
  public void onClick(ClickEvent event) {
    Window.Location.assign("http://www.someurl.com");
  }
});
rootPanel.add(button);

Upvotes: 11

Uri
Uri

Reputation: 89839

You need to have your event handler request a URL change, not have it add a new widget.

Do you want to open it in a new window or in the same window?

Generally speaking, you would probably want to have your click handler code use the History class or the Window class to open a different URL and/or to manipulate the browser history.

Upvotes: 0

Related Questions