GWT 的标题框架面板(使用 FIELDSET 和 LEGEND html 标签)
我正在尝试在 GWT 中创建一个带标题的边框框架,结果如下:
这可以使用HTML 字段集和图例标记,例如
<fieldset>
<legend>Connection parameters</legend>
... the rest ...
</fieldset>
我想在 GWT 中创建一个实现该功能的自定义小部件。我设法做到了这一点,但问题是,尽管我添加了处理程序,但小部件内部发生的事件(按钮单击等)不会被触发。
我的小部件的实现如下:
public class TitledPanel extends Widget {
private Element legend;
private Widget content = null;
public TitledPanel() {
Element fieldset = DOM.createFieldSet();
legend = DOM.createLegend();
DOM.appendChild(fieldset, legend);
setElement(fieldset);
}
public TitledPanel(String title) {
this();
setTitle(title);
}
@Override
public String getTitle() {
return DOM.getInnerHTML(legend);
}
@Override
public void setTitle(String html) {
DOM.setInnerHTML(legend, html);
}
public Widget getContent() {
return content;
}
public void setContent(Widget content) {
if (this.content != null) {
DOM.removeChild(getElement(), this.content.getElement());
}
this.content = content;
DOM.appendChild(getElement(), content.getElement());
}
}
我是否需要扩展 Composite,或者需要手动重新路由事件,或者还有其他方法吗?
I'm trying to create a titled border frame in GWT, which results in this:
This can be done using HTML fieldset and legend tags, such as
<fieldset>
<legend>Connection parameters</legend>
... the rest ...
</fieldset>
I want to create a custom widget in GWT that implements that. I managed to do that, but the problem is that events that happen inside the widget (button click etc) does not get fired although I have added the handler.
My implementation of the widget is as follows:
public class TitledPanel extends Widget {
private Element legend;
private Widget content = null;
public TitledPanel() {
Element fieldset = DOM.createFieldSet();
legend = DOM.createLegend();
DOM.appendChild(fieldset, legend);
setElement(fieldset);
}
public TitledPanel(String title) {
this();
setTitle(title);
}
@Override
public String getTitle() {
return DOM.getInnerHTML(legend);
}
@Override
public void setTitle(String html) {
DOM.setInnerHTML(legend, html);
}
public Widget getContent() {
return content;
}
public void setContent(Widget content) {
if (this.content != null) {
DOM.removeChild(getElement(), this.content.getElement());
}
this.content = content;
DOM.appendChild(getElement(), content.getElement());
}
}
Do I need to extend Composite, or need to manually reroute the events, or is there other ways?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您正在寻找 标题面板:
I think you're looking for CaptionPanel:
我认为这里的问题是您只调用
DOM.appendChild
- 这不会导致TitledPanel
采用Widget
。正常的操作过程是扩展Composite
,然后调用initWidget(Widget widget)
- 在内部调用widget.setParent(this);
code>,这又使父级采用此小部件并将其附加到浏览器的文档中。但是 com.google.gwt.user.client.ui.Widget.setParent(Widget) 仅在包中可见,因此您无法从代码中调用它(例如,在DOM.appendChild
之后) )。我建议阅读小部件最佳实践/小部件构建< /a>,尤其是自行清理和/或查看某些 GWT 小部件的源代码,以了解 GWT 如何查看自定义小部件创建。
并且,正如罗伯特建议的< /a>,CaptionPanel 是更安全的路线:)
I think the problem here is that you just call
DOM.appendChild
- this doesn't cause theTitledPanel
to adopt theWidget
. The normal course of action is that you extendComposite
and then callinitWidget(Widget widget)
- inside the hood it callswidget.setParent(this);
, which in turn makes the parent adopt this widget and attach it to the browser's document. However com.google.gwt.user.client.ui.Widget.setParent(Widget) is only package-visible so you can't call it from your code (after, for example,DOM.appendChild
).I'd recommend reading Widget Best Practices / Widget Building, especially the Clean up after yourself and/or look at the source code for some GWT Widgets, to get the idea how the GWT sees custom widget creation.
And, as Robert suggested, CaptionPanel is the safer route :)