TimeField - by extending CustomField

Hi Vaadin Masters :slight_smile:

I would like to create custom component, with which user will be able to choose proper hour from the dropdown list. Component should save data in date type. For now I have recognized that the best option to do this is use the CustomField component which is build in vaadin 7 api.

So far I created the class TimeField as below:

public class TimeField extends CustomField<Date> {

	private static final long serialVersionUID = 5683436290908893049L;
	Date time = new Date();
	SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

	public TimeField() {
	}

	public TimeField(String caption) {
		setCaption(caption);
	}

	@Override
	protected Component initContent() {
		ComboBox timeField = new ComboBox();
		timeField.setImmediate(true);

		List<String> hours = Arrays.asList("00:00", "01:00", "02:00", "03:00",
				"04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00",
				"11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00",
				"18:00", "19:00", "20:00", "21:00", "22:00", "23:00");

		for (String s : hours) {
			try {
				time = sdf.parse(s);
				timeField.addItem(time);
				timeField.setItemCaption(time, s);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		timeField.addValueChangeListener(new ValueChangeListener() {

			private static final long serialVersionUID = 8381159580140256156L;

			@Override
			public void valueChange(
					com.vaadin.data.Property.ValueChangeEvent event) {
				try {
					time = sdf.parse((String) event.getProperty().toString());
					setValue(time);
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		});

		return timeField;
	}

	@Override
	public Class<Date> getType() {
		// TODO Auto-generated method stub
		return null;
	}

}

I want to use it in my form but, while component is initializing I am getting this error:


Caused by: java.lang.NullPointerException
	at java.lang.Class.isAssignableFrom(Native Method)
	at com.vaadin.data.util.converter.DefaultConverterFactory.createDateConverter(DefaultConverterFactory.java:88)
	at com.vaadin.data.util.converter.DefaultConverterFactory.findConverter(DefaultConverterFactory.java:77)
	at com.vaadin.data.util.converter.DefaultConverterFactory.createConverter(DefaultConverterFactory.java:52)
	at com.vaadin.data.util.converter.ConverterUtil.getConverter(ConverterUtil.java:55)
	at com.vaadin.ui.AbstractField.setConverter(AbstractField.java:673)
	at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:621)
	at com.vaadin.data.fieldgroup.FieldGroup.bind(FieldGroup.java:265)
	at com.example.grafikmaker.UI.view.manager.RegEditShiftPanel.buildRegEditForm(RegEditShiftPanel.java:125)
	at com.example.grafikmaker.UI.view.manager.RegEditShiftPanel.buildPanel(RegEditShiftPanel.java:93)
	at com.example.grafikmaker.UI.view.manager.RegEditShiftPanel.<init>(RegEditShiftPanel.java:52)
	at com.example.grafikmaker.UI.view.manager.ShiftsView$1.buttonClick(ShiftsView.java:136)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
	... 32 more

I’ve been searching through vaadin forum for the answer on this error but unfortunately I haven’t found anything.

…or you could use the
TimeField
add-on I made :slight_smile: You can look at the sources, at least.

My God, I’ve found the bug. I’ve lost on it one day

The method
getType
which we have to Override should return Date.class as below, kill mee :slight_smile:


@Override
public Class<Date> getType() {
// TODO Auto-generated method stub
return Date.class;
}

Yes, do beware of auto-generated method stubs :slight_smile: