如何连接GWT超链接点击处理程序? [英] How to wire up GWT hyperlink click handler?

查看:176
本文介绍了如何连接GWT超链接点击处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是GWT的新手,并试图实现以下目标:


下面是我熟悉的代码:



pre> public class MyWebApp实现EntryPoint {
//用户看到的所有内容的主容器(视图)
private LayoutPanel mainPanel;

//标题(MyWebApp)和后续的< hr />的简单HTML
私人SafeHtml标题;

//三个链接Dashboard,Monitors和Help Desk
私人Horizo​​ntalPanel navMenu;

//用户单击
// 3个链接之一时填充的空白内容。
私人面板menuContent;

@Override
public void onModuleLoad(){
//最初的片段包含标题,导航菜单和空的contentdiv。
//每个菜单/屏幕然后填写内容div。
initMainPanel();

RootPanel.get()。add(mainPanel);


private void initMainPanel(){
SafeHtmlBuilder headerBuilder = new SafeHtmlBuilder();
navMenu =新的Horizo​​ntalPanel();

//留空,直到用户点击3个菜单中的一个。
//然后菜单将决定为
//此面板注入哪个面板。
menuContent = null;

//为标题创建简单的HTML。
headerBuilder.append(< h1> MyWebApp< / h1>< hr />);

//创建navMenu项目。
超链接dashboardLink,monitorsLink,helpDeskLink;

//主页是http://www.mywebapp.com
//我希望dashboardLink将menuContent和重定向用户注入
// http:// www.mywebapp.com/dashboard
dashboardLink =新的超链接(???,???);

// http://www.mywebapp.com/monitors
monitorsLink =新的超链接(???,???);

// http://www.mywebapp.com/help-desk
helpDeskLink =新的超链接(???,???);
navMenu.add(dashboardLink);
navMenu.add(monitorsLink);
navMenu.add(helpDeskLink);

//将所有小部件添加到mainPanel。
mainPanel.add(new HTML(headerBuilder.toSafeHtml()。toString()));
mainPanel.add(navMenu);
mainPanel.add(menuContent);

//定位和调整窗口小部件(为了简洁起见省略)。
// mainPanel.setWidgetHorizo​​ntalPosition(...);
}

private HTML getDashboardMenuContent(){
return new HTML(This is the dashboard。);
}

private HTML getMonitorsMenuContent(){
return new HTML(These are the monitors。);
}

private HTML getHelpDeskMenuContent(){
return new HTML(This is the help desk。);


$ / code>

最重要的是:


  • 如何连接超链接 s,以便当用户点击它们时,我可以调用适当的 getXXXMenuContent()方法,然后将它添加到 menuContent



但也:


  • 感觉像我在做这里有一些错误: mainPanel.add(new HTML(headerBuilder.toSafeHtml()。toString())); - 如果是的话是什么?!?我应该如何以安全的方式添加简单的< h1> < hr /> 因此使用Safe *对象),高效并符合推荐实践?

  • 我应该在这里实现 UiBinder 吗?如果是这样,我会为每个菜单的内容还是整个 mainPanel 或两者都制作 UiBinder s?



预先致谢! (b

  dashboardLink.addClickHandler(
new ClickHandler()
{
public void onClick( ClickEvent事件)
{
mainPanel.setWidget(getDashboardMenuContent());
}
});

您应该注意 Hyperlink.addClickHandler(...),建议使用 Anchor.addClickHandler(...)



至于其他问题:使用UIBinder创建UI的过程更加优雅和容易,所以一定要仔细研究一下,但是要尽量让它先工作,以避免增加的复杂性。 ui.xml 设置: - )



干杯,


I am brand new to GWT and am trying to achieve the following:

Here's the code that I've cooked up:

public class MyWebApp implements EntryPoint {
    // The main container for everything the user sees (the "view")
    private LayoutPanel mainPanel;

    // Simple HTML for the header ("MyWebApp") and subsequent <hr/>
    private SafeHtml header;

    // The three links "Dashboard", "Monitors" and "Help Desk"
    private HorizontalPanel navMenu;

    // The empty content that gets populated when user clicks one of
    // the 3 links.
    private Panel menuContent;

    @Override
    public void onModuleLoad() {
        // The initial fragment contains the header, nav menu and empty "content" div.
        // Each menu/screen then fills out content div.
        initMainPanel();

        RootPanel.get().add(mainPanel);
    }

    private void initMainPanel() {
        SafeHtmlBuilder headerBuilder = new SafeHtmlBuilder();
        navMenu = new HorizontalPanel();

        // Leaving null until user clicks on one of the 3 menus.
        // Then the menu will decide what panel gets injected for
        // this panel.
        menuContent = null;

        // Create the simple HTML for the header.
        headerBuilder.append("<h1>MyWebApp</h1><hr/>");

        // Create the navMenu items.
        Hyperlink dashboardLink, monitorsLink, helpDeskLink;

        // Homepage is http://www.mywebapp.com
        // I want the dashboardLink to inject menuContent and "redirect" user to
        // http://www.mywebapp.com/dashboard
        dashboardLink = new Hyperlink("???", "???");

        // http://www.mywebapp.com/monitors
        monitorsLink = new Hyperlink("???", "???");

        // http://www.mywebapp.com/help-desk
        helpDeskLink = new Hyperlink("???", "???");
        navMenu.add(dashboardLink);
        navMenu.add(monitorsLink);
        navMenu.add(helpDeskLink);

        // Add all widgets to the mainPanel.
        mainPanel.add(new HTML(headerBuilder.toSafeHtml().toString()));
        mainPanel.add(navMenu);
        mainPanel.add(menuContent);

        // Position and size the widgets (omitted for brevity).
        // mainPanel.setWidgetHorizontalPosition(...);
    }

    private HTML getDashboardMenuContent() {
        return new HTML("This is the dashboard.");
    }

    private HTML getMonitorsMenuContent() {
        return new HTML("These are the monitors.");
    }

    private HTML getHelpDeskMenuContent() {
        return new HTML("This is the help desk.");
    }
}

Most importantly:

  • How do I "wire up" the Hyperlinks so that when the user clicks them, I can call the appropriate getXXXMenuContent() method, and then add that to menuContent?

But also:

  • I feel like I'm doing something wrong here: mainPanel.add(new HTML(headerBuilder.toSafeHtml().toString())); - if so what is it?!? How should I be adding a simple <h1> and <hr/> in a way that's secure (hence the use of the Safe* objects), efficient, and conforming to recommended practices?
  • Should I be implementing UiBinder here? If so, would I make UiBinders for each menu's content or for the entire mainPanel, or both?

Thanks in advance!

解决方案

Something like

dashboardLink.addClickHandler( 
   new ClickHandler() 
   {
       public void onClick( ClickEvent event ) 
       {
           mainPanel.setWidget( getDashboardMenuContent() );
       }
   } );

You should note that Hyperlink.addClickHandler(...) is deprecated and it is recommended to use Anchor.addClickHandler(...) instead.

As for the other questions: It is a lot more elegant and easier to build UI's with UIBinder, so definitely look into that, but do try to make "it" work first to avoid the added complexity of the .ui.xml setup :-)

Cheers,

这篇关于如何连接GWT超链接点击处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆