Hi,
I would like to use a SpinnerItem to edit a value between 0 and 1 by step of 0.01. The value should be displayed and edited in %.
I would like it to be possible to edit either with the up/down arrows or typing the value (with a %) directly in the text box.
With the code below, editing by typing works well, however it does not work using the arrows. Not using the parser (using only Formatter) makes it work with arrows but not direct edition. Any advice? thanks!
I would like to use a SpinnerItem to edit a value between 0 and 1 by step of 0.01. The value should be displayed and edited in %.
I would like it to be possible to edit either with the up/down arrows or typing the value (with a %) directly in the text box.
With the code below, editing by typing works well, however it does not work using the arrows. Not using the parser (using only Formatter) makes it work with arrows but not direct edition. Any advice? thanks!
Code:
private static final boolean NEED_GWT_4670_FIX; static { NumberFormat nf = NumberFormat.getFormat("#%"); NEED_GWT_4670_FIX = nf.parse(nf.format(1d)) == 100d; } public void quickTest(){ DynamicForm form = new DynamicForm(); SpinnerItem field = new SpinnerItem("test"); field.setCanEdit(true); field.setValue(0.5); field.setMin(0.0); field.setMax(1.0); field.setStep(0.01); field.setEditorValueFormatter(new FormItemValueFormatter(){ @Override public String formatValue(Object value, Record record, DynamicForm form, FormItem item) { if(value == null) return null; String val = null; try { NumberFormat df = NumberFormat.getFormat("#,##0%"); val = df.format(((Number) value).doubleValue()); } catch (Exception e) { return value.toString(); } return val; } }); field.setEditorValueParser(new FormItemValueParser(){ @Override public Object parseValue(String value, DynamicForm form, FormItem item) { if(value == null) return null; Double val = null; try { NumberFormat df = NumberFormat.getFormat("#,##0%"); val = df.parse(value); if (NEED_GWT_4670_FIX) val = val / 100d; //This is a workaround for a known GWT bug } catch (Exception e) { return null; } return val; } }); form.setItems(field); form.draw(); }
Comment