Convertors

Hi,

I am struggling to understand convertors. I have a ComboBox which displays a series of date populated via a data source. I want the object id to be a java.util.Date but display the month only ‘July’, etc.

I thought that customising a Convertor should do the trick.

I have extended the StringToDateConvertor and overriden the protected DateFormat getFormat(Locale locale) to return a SimpleDateFormat with the mm pattern. I have set the convertor on the combobox via combo.setConverter(MonthOnlyStringToDateConvertor.class);

This doesn’t do anything. It still displays the full date. Should this work or am I not grasping the concept?

Thanks
Simon

Did you also customize the ConverterFactory? Another way would be to set a concrete Instance of your Converter via ComboBox#setConverter(new MonthOnlyConverter()).

Thanks for your response.

I haven’t used a custom ConverterFactory. As far as I understand (which isn’t much), this would effect all conversions where from Date → String, which I only want this particular instance.

I also tried the concrete instance, but it wouldn’t compile. This is because the ComboBox extends the AbstractField with the generic type of Object, whereas the type for the custom converter is Date

A converter works between the value of a Field and its data source, whereas here your field value (item ID) is the Date.

The ComboBox component supports a few different item caption modes, in your case the item ID toString() mode is probably used.

If you have a limited number of elements, the easiest option would probably be specifying explicit captions for every item by calling ComboBox.setItemCaption(id, caption) when you populate the ComboBox, or in a listener for the container if the content can change on the fly.

Ok that’s great thanks.