如何在GWT UiBinder中引用Widget?

时间:2016-02-11 19:50:38

标签: java gwt uibinder

是否可以引用UiBinder创建的窗口小部件并将其传递给* .ui.xml文件中的其他窗口小部件的设置者?

我尝试使用代码创建一个标签(CustomLabel),该标签可以监听并跟踪表单小部件的状态(CustomWidget)。问题是GWT编译失败了:

[ERROR] Exception in thread "main" java.lang.StackOverflowError
[ERROR]     at sun.nio.cs.UTF_8$Encoder.encodeLoop(UTF_8.java:691)
[ERROR]     at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:579)
[ERROR]     at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:271)
[ERROR]     at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
[ERROR]     at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
[ERROR]     at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:129)
[ERROR]     at java.io.BufferedWriter.flush(BufferedWriter.java:253)
[ERROR]     at java.io.PrintWriter.newLine(PrintWriter.java:482)
[ERROR]     at java.io.PrintWriter.println(PrintWriter.java:629)
[ERROR]     at java.io.PrintWriter.println(PrintWriter.java:740)
[ERROR]     at com.google.gwt.dev.util.log.PrintWriterTreeLogger.doLog(PrintWriterTreeLogger.java:79)
[ERROR]     at com.google.gwt.dev.util.log.PrintWriterTreeLogger.doCommitBranch(PrintWriterTreeLogger.java:65)
[ERROR]     at com.google.gwt.dev.util.log.AbstractTreeLogger.commitMyBranchEntryInMyParentLogger(AbstractTreeLogger.java:256)
[ERROR]     at com.google.gwt.dev.util.log.AbstractTreeLogger.branch(AbstractTreeLogger.java:167)
[ERROR]     at com.google.gwt.core.ext.TreeLogger.branch(TreeLogger.java:212)
[ERROR]     at com.google.gwt.editor.rebind.model.EditorModel.createEditorData(EditorModel.java:381)
[ERROR]     at com.google.gwt.editor.rebind.model.EditorModel.createEditorData(EditorModel.java:390)
[ERROR]     at com.google.gwt.editor.rebind.model.EditorModel.createEditorData(EditorModel.java:390)

View.ui.xml

<g:HTMLPanel>
    <x:CustomLabel forTarget={widget} />
    <x:CustomWidget ui:field="widget" />
</g:HTMLPanel>

View.java

@UiField
CustomWidget widget;

CustomLabel.java

public void setForTarged(CustomWidget forTarget) {
    this.forTarget = forTarget
}

2 个答案:

答案 0 :(得分:1)

原则上这应该有效。我在我的代码中使用类似的引用。

在View.java中:

@UiField
CustomWidget customWidget; 

应该是

@UiField
CustomWidget widget;

答案 1 :(得分:0)

不,你不能这样做 - 当UiBinder调用你的setForTarget方法时,还没有可用的小部件。

您可以在初始化自定义窗口小部件后在代码中调用此方法。