我正在尝试创建一个小部件,该小部件将以与本机值不同的格式呈现其关联值。例如,如果值(在数据库中)是“abcde”,我想在屏幕上显示“ab.cd.e”,如果用户键入“abcde”,我也想显示“ab.cd.e” ”。如果用户键入“ab.cd.e”,那么我只想在数据库中存储“abcde”。我在 GWT 中这样做editor framework
。我试图使用这个答案的建议:Converting String to BigDecimal in GWT,但我无法让它工作。这是我在 UiBinder 文件中的内容:
<g:TextBox ui:field='myTextBox' width='300px'/>
在相关的 Java 单元中:
@UiField
TextBox myTextBox;
...
initWidget(binder.createAndBindUi(this));
new MyValueBox(myTextBox);
这是 MyValueBox 小部件的定义:
public class MyValueBox extends ValueBox<String> {
//=========================================================================
public static class MyRenderer extends AbstractRenderer<String> {
private static MyRenderer _instance;
private static MyRenderer instance() {
if (_instance == null) {
_instance = new MyRenderer();
}
return _instance;
}
@Override
public String render(final String text) {
// validation is required before doing this!
return text.substring(0, 2) + "." + text.substring(2, 4) + "."
+ text.substring(4);
}
}
//=========================================================================
public static class MyParser implements Parser<String> {
private static MyParser _instance;
private static MyParser instance() {
if (_instance == null) {
_instance = new MyParser();
}
return _instance;
}
@Override
public String parse(final CharSequence text) throws ParseException {
return "parsed string";
}
}
//=========================================================================
public MyValueBox(final TextBox valueBox) {
super(valueBox.getElement(), MyRenderer.instance(), MyParser.instance());
}
}
如您所见,我正在尝试包装TextBox
使用 创建的UiBinder
,但我没有看到任何效果。我知道我遗漏了一些非常简单的东西,并且有一种更简单的方法可以实现这一点,但我很难过。感谢您的任何建议!
- 编辑 -
我最终决定使用 a CellWidget
,它还有一个额外的优势,即除了在面板上使用它之外,我还可以在单元格小部件(例如 DataGrid)中使用此代码。我在这里记录了我的解决方案:GWT: A Custom Cell Example