如何在超链接中嵌入 GWT 小部件?

How to embed a GWT widget in a hyperlink?

我需要实现以下方法:

public static Widget embedInHyperlink(String href, Widget content);

该方法需要创建一个小部件,在网站上呈现为<a href="...">[content]</a>

内容可以是包含图像文本和其他小部件的复杂小部件。

我的第一个解决方案是创建一个 FocusPanel 并添加一个 ClickHandler,但是用户无法 [Ctrl]-单击小部件以在新的浏览器选项卡中将其打开。当我的内容小部件未嵌入超链接时,右键单击和 "Open in new Tab" 或 "Save as.." 也是不可能的。

您可以改用或扩展 HTML widget to construct your elements. If you can avoid using child widgets, you will be better off, as widgets are typically not thoroughly tested to work correctly when wrapped by a href. If you must use child widgets you can use a HTMLPanel

public class AnchorPanel extends SimplePanel {

    public AnchorPanel(IsWidget content, String href, String target) {
        super(DOM.createAnchor());
        getElement().setAttribute("href", href);
        getElement().setAttribute("target", target);
        add(content);
    }

}