我正在尝试从MenuBar替换DockLayoutPanel的CENTER部分的内容。 MenuBar将位于北方,但“内容”(表格,报告等)将位于中心。
我想我可以通过从RootPanel抓取整个DockLayoutPanel(索引0,因为那是直接连接到它的唯一小部件),然后以某种方式删除中心的当前内容并插入新内容。不幸的是,getWidget(int index)是非静态的,所以它不起作用。
任何人都可以用正确的方式做到这一点吗?非工作代码如下:
// snipped package and import details
public class menubar extends Composite {
private static menubarUiBinder uiBinder = GWT.create(menubarUiBinder.class);
interface menubarUiBinder extends UiBinder<Widget, menubar> {
}
@UiField MenuBar applicationMenuBar;
@UiField MenuBar processMenuBar;
@UiField MenuBar reportsMenuBar;
@UiField MenuItem addPowerRequirementCmd;
@UiField MenuItem powerUsageReportCmd;
public menubar() {
initWidget(uiBinder.createAndBindUi(this));
// command to replace whatever is in DockLayoutPanel.CENTER w/ AddPowerRequirement
// FormPanel
addPowerRequirementCmd.setCommand(new Command() {
@Override
public void execute() {
// get DockLayoutPanel from the RootPanel (it's the only widget, should be
// index 0
DockLayoutPanel dlp = (DockLayoutPanel) RootPanel.getWidget(0);
// clear out DockLayoutPanel.CENTER
// insert the FormLayoutPanel into DockLayoutPanel.CENTER
dlp.add(new PowerRequirementForm());
}
});
// command to replace whatever is in DockLayoutPanel.CENTER w/ power usage report
powerUsageReportCmd.setCommand(new Command() {
@Override
public void execute() {
}
});
}
}
谢谢!
答案 0 :(得分:4)
使用ui:field
属性,就像对MenuBar一样,从UiBinder获取对DockLayoutPanel的引用:
<g:DockLayoutPanel ui:field="dockPanel"/>
并在课堂上:
@UiField DockLayoutPanel dockPanel;
但是,听起来您希望根据应用程序状态显示一组显示在面板中央的小部件。 DeckPanel是一个更好的解决方案:
<g:DockLayoutPanel>
<g:center>
<g:DeckPanel ui:field="deck">
<g:FlowPanel>Panel #0</g:FlowPanel>
<g:FlowPanel>Panel #1</g:FlowPanel>
</g:DeckPanel>
</g:center>
</g:DockLayoutPanel>
然后切换DeckPanel的显示子项:
deck.showWidget(1); // Show Panel #1
deck.showWidget(0); // Show panel #0