开发者

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);

t开发者_JAVA百科hanks in advance !


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);


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')
    }-*/;


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜