【问题标题】:How to change the html of a HTMLPanel如何更改 HTMLPanel 的 html
【发布时间】:2012-10-20 22:01:33
【问题描述】:

我想声明一个 HTMLPanel 的子类。 在它的构造函数中,我想给它一些参数来构造包含的 html。

因为我必须调用超级构造函数作为第一个语句,所以我必须稍后在构造函数中更改 html。

我该怎么做?

public class MyHTMLPanel extends HTMLPanel
{ 
  public MyHTMLPanel(String id, int anotherParameter)
  { super("");
     String html=""
    // ... some code th construct the html
    //??? this.setHtml(html);  
  }  
}

【问题讨论】:

    标签: java html gwt panel


    【解决方案1】:

    您可以在下面找到一个我使用过并且对我很有效的示例。 我不记得为什么我不继承 HTMLPanel,无论是否有充分的理由。 如果您在一个页面中包含多个相同类型的对象,您会注意到一种随机化 html id 的机制。

    public abstract class HtmlPanelBase extends Composite
    {
      private String _dynPostfix = "";
      protected final String id(final String staticId) { return staticId + _dynPostfix; }
      private final String wrapId(final String id) { return "id=\"" + id + "\""; }
      private final String wrapDynId(final String refId) { return wrapId(id(refId)); }
    
      private String _htmlAsText = null;
      public String getHtmlAsText() { return _htmlAsText; }
    
      abstract protected String htmlPanelBundleHtmlText();
      abstract protected List<String> idList();
    
      protected HTMLPanel _holder = null;
      private HTMLPanel createHtmlPanel(final boolean defineGloballyUniqueIds)
      {
        // Referent HTML panel text containing the reference id's.
        _htmlAsText = htmlPanelBundleHtmlText();
        if (defineGloballyUniqueIds)
        {
          // List of id's in the HTML Panel reference page to replace with dynamic/unique id's.
          final List<String> refIdList = idList();
          // Replace the reference id's with dynamic/unique id's.
          for (String refId : refIdList)
            _htmlAsText = _htmlAsText.replace(wrapId(refId), wrapDynId(refId));
        }
        // Return the HTMLPanel containing the globally unique id's.
        return new HTMLPanel(_htmlAsText);
      }
      public HtmlPanelBase(final boolean defineGloballyUniqueIds)
      {
        setup(defineGloballyUniqueIds);
        initWidget(_holder);
      }
    
      private void setup(final boolean defineGloballyUniqueIds)
      {
        if (defineGloballyUniqueIds)
          _dynPostfix = "_" + UUID.uuid().replace("-", "_");
        _holder = createHtmlPanel(defineGloballyUniqueIds);
      }
    }
    

    现在你如何从上面的基础子类化:

    public class HtmlPanelTemplate extends HtmlPanelBase
    {
      private final static boolean _defineGloballyUniqueIds = false;
      private final static int _numIdCapacity = 40;
    
      public HtmlPanelTemplate()
      {
        super(_defineGloballyUniqueIds);
        setup();
      }
    
      @Override
      protected String htmlPanelBundleHtmlText()
      {
        return YourClientBundle.INSTANCE.getYourFileHtml().getText();
      }
    
      @Override
      protected List<String> idList()
      {
        final List<String> idList = new ArrayList<String>(_numIdCapacity);
        return idList;
      }
    
      private void setup()
      {
      }
    }
    

    【讨论】:

      【解决方案2】:

      您不需要继承 HTMLPanel。您可以创建一个简单的 Composite 小部件:

      public class myPanel extends Composite {
      
          private HTMLPanel panel = new HTMLPanel();
      
          public myPanel(String id, int anotherParameter) {
              // set HTML to panel based on your parameters
              initWidget(panel);
          }
      }
      

      【讨论】:

      • 这如何回答这个问题?
      • 它没有回答问题。
      • 它为问题中发布的问题提供了解决方案。您可能更喜欢不同的解决方案 - 这是您的特权,但否决它是很粗鲁的。下一个答案提供了相同的方法(扩展了 Composite)并且有 3 个赞成票。
      • 我认为您的回答实际上并没有回答问题,因此投反对票。这一点也不粗鲁。它遵循规则。您从未在答案中设置 HTML,这就是 OP 的问题——“//??? this.setHtml(html);”而你没有回答。
      【解决方案3】:
      htmlPanel.getElement().setInnerHTML(...)
      

      不知道这是否适用于派生类的构造函数。但是为特定的内容文本设置一个类并不是一个好的解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-04
        • 1970-01-01
        • 2011-07-19
        相关资源
        最近更新 更多