Reputation: 189
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
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
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
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