views:

53

answers:

1

Hi, I'm trying to create a JSpinner to enable the user to pick a Date. I want there to be a lower date limit and an upper date limit. I also want the initial value to be the lower date limit. Unfortunately, My problem is that it won't let me use the lower limit as the initial value (the JSpinner simply becomes unresponsive). Here is my code:

SpinnerDateModel model = new SpinnerDateModel();
model.setStart(minTime);  //lower limit
model.setEnd(maxTime);    //upper limit
model.setValue(minTime);  //doesn't like this!
model.setCalendarField(Calendar.MINUTE);
JSpinner timePicker = new JSpinner(model);
timePicker.setEditor(new JSpinner.DateEditor(timePicker, "HH:mm dd/MM/yy"));

If I set the initial value to be one minute before or after the lower limit, it works fine. But for my requirements, I do not want that.

Help?

A: 

Looks like a bug to me. I messed around with some code and it appears that whatever you pass to setValue, I'll call it value, must be at least one calendarField unit greater than minTime.

i.e. if you had used model.setCalendarField(Calendar.YEAR), value would have to be any date in 2011, assuming you used a date in 2010 for minTime.

According to Sun, the invariant enforced by the SpinnerDateModel constructors is minimum <= value <= maximum, so this problem shouldn't be happening.

The first workaround that comes to mind is creating a custom SpinnerDateModel which overrides the getPreviousValue() and setValue() methods to manually check against your desired minTime.

dpatch