Yes, you're right, but this way fit 90% of my needs (It's a Label...) Otherwise, it's possible to use a CustomConverter to do it, and then override getConverter on Component. So, it also work on TextField (but i agree, it's not exactly your use case. ) :
@Override public IConverter getConverter(Class<?> type) { return PercentConverter.INSTANCE_2DEC; } public class PercentConverter extends AbstractNumberConverter { public static final PercentConverter INSTANCE = new PercentConverter(); public static final PercentConverter INSTANCE_2DEC = new PercentConverter() { public NumberFormat getNumberFormat(Locale locale) { NumberFormat nf = DecimalFormat.getPercentInstance(locale); nf.setMinimumFractionDigits(2); nf.setMaximumFractionDigits(2); return nf; }; }; public static final PercentConverter INSTANCE_4DEC = new PercentConverter() { public NumberFormat getNumberFormat(Locale locale) { NumberFormat nf = DecimalFormat.getPercentInstance(locale); nf.setMinimumFractionDigits(4); nf.setMaximumFractionDigits(4); return nf; }; }; public Object convertToObject(String value, Locale locale) { if (value == null) return null; value = value.trim(); if (value.trim().equals("")) { return null; } Number n = null; try { n = getNumberFormat(locale).parse(value); } catch (ParseException e) { throw newConversionException(e.getMessage(), value, locale).setFormat( getNumberFormat(locale)); } return n.floatValue(); } @Override protected Class<?> getTargetType() { return Float.class; } @Override public NumberFormat getNumberFormat(Locale locale) { return NumberFormat.getPercentInstance(locale); } } 2011/9/23 Pranav kacholia <pranav.kacho...@gmail.com> > Suppose someone wants to call formatlLabel.getModelObject() ... > > I don’t see why they would, but hypothetically. > > In this case you would get the formatted string. Since the label is merely > a view, should it be changing the model even by formatting it? > > > > From: Yves-Marie LAINÉ [via Apache Wicket] [mailto: > ml-node+s1842946n3837008...@n4.nabble.com] > Sent: 23 September 2011 19:57 > To: Pranav kacholia > Subject: Re: DecimalFormatLabel (proposed) > > > > Hi all, > > Personnaly I did it like that (simpler for i18n, i think) : > > public class FormatLabel extends Label { > > public FormatLabel(String id,final String formatKey, final IModel<? > extends Serializable> model) { > super (id); > setDefaultModel(new AbstractReadOnlyModel<String>() { > @Override > public String getObject() { > > Serializable o = model.getObject(); > > if ( o != null) { > return new StringResourceModel(formatKey, > FormatLabel.this, model, new Object[]{o}).getObject(); > } > > return getString(FormatLabel.this.getId() + ".null"); > } > }); > } > } > > > Yves-Marie > > > > 2011/9/23 Pranav kacholia <[hidden email]> > > > > Perhaps we can keep it as a NumberFormatLabel for greater flexibility > > > > import java.text.NumberFormat; > > import org.apache.wicket.markup.ComponentTag; > > import org.apache.wicket.markup.MarkupStream; > > import org.apache.wicket.markup.html.basic.Label; > > import org.apache.wicket.model.IModel; > > import org.apache.wicket.model.Model; > > > > /** > > * > > * @author pkacholia > > */ > > public class NumberFormatLabel extends Label { > > > > final private NumberFormat formatter; > > > > public NumberFormatLabel(String id, IModel<? extends Number> model, > > NumberFormat format) { > > super(id, model); > > formatter = format; > > } > > > > public NumberFormatLabel(String id, Number obj, NumberFormat format) > { > > this(id, new Model<Number>(obj), format); > > } > > > > public NumberFormatLabel(String id, NumberFormat format) { > > super(id); > > formatter = format; > > } > > > > /** > > * {@inheritDoc} > > */ > > @Override > > public void onComponentTagBody(final MarkupStream markupStream, final > > ComponentTag openTag) { > > String response; > > try { > > response = formatter.format(getDefaultModelObject()); > > } catch (IllegalArgumentException ex) { > > error(getString("NaN")); > > response = ""; > > } catch (NullPointerException ex) { > > error(getString("NullFormatter")); > > response = ""; > > } > > replaceComponentTagBody(markupStream, openTag, response); > > } > > } > > > > -- > > View this message in context: > > > http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3836001.html > > Sent from the Users forum mailing list archive at Nabble.com. > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: [hidden email] > > For additional commands, e-mail: [hidden email] > > > > > > > > -- > Yves-Marie LAINÉ > > > > _____ > > If you reply to this email, your message will be added to the discussion > below: > > > http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3837008.html > > To unsubscribe from DecimalFormatLabel (proposed), click < > http://apache-wicket.1842946.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=3834813&code=cHJhbmF2LmthY2hvbGlhQGdtYWlsLmNvbXwzODM0ODEzfC0yMDAxMDYyNzM1> > here. > > > > -- > View this message in context: > http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3837115.html > Sent from the Users forum mailing list archive at Nabble.com. > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org > For additional commands, e-mail: users-h...@wicket.apache.org > > -- Yves-Marie LAINÉ