我正在嘗試創建一個小部件,它將以與本機值不同的格式呈現其關聯值。例如,如果數值(在數據庫中)的值是「abcde」,我想在屏幕上顯示「ab.cd.e」,如果用戶鍵入「abcde」,我也想顯示「ab.cd.e 」。如果用戶鍵入「ab.cd.e」,那麼我想在數據庫中只存儲「abcde」。我正在GWT editor framework
之內這樣做。我試圖使用這個答案的建議:Converting String to BigDecimal in GWT,但我不能得到它的工作。下面是我在UiBinder的文件:GWT中的自定義渲染器
<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());
}
}
正如你所看到的,我試圖包裝使用UiBinder
創建的TextBox
,但我沒有看到任何效果。我知道我錯過了一件非常簡單的事情,而且有一種更簡單的方法來完成這個任務,但我很難過。謝謝你的任何建議!
- 編輯 -
我最終決定使用CellWidget
,其中有額外的好處,我可以在單元格控件(例如,一個DataGrid)使用此代碼,除了使用它在面板上。我在這裏記錄了我的解決方案:GWT: A Custom Cell Example
我不是如何創建MyValueBox實例明確的 - MyValueBox類有一個構造函數一個參數,我當然可以刪除,但後來我還是需要調用繼承的構造,這需要一個'Element'作爲第一個參數......我該怎麼做? –
你爲什麼要在你的構造函數中發送一個TextBox?我可以看到MyValueBox的構造函數嗎?不過,您可以查看http://www.gwtproject.org/doc/latest/DevGuideUiBinder.html#Using_a_widget瞭解如何通過構造函數發送參數。 – amaurs
我將'TextBox'發送給構造函數,以便將'TextBox'封裝在具有渲染器的窗口小部件中。我沒有看到重寫'TextBox'上的渲染的方法。 –