1

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?

3
  • This is not a direct answer to your question, but you might want to try to use the GUI builder in NetBeans. It saved me a lot of time from configuring swing components by code. In your case, you can create a JSpinner and configure the "model" from the properties panel. Aug 13, 2010 at 17:02
  • Maybe it saves you time, but I actually find it quicker (and the resultant code a lot cleaner/organised) doing it manually. I don't see how that would solve this problem anyway.
    – Steve Jobs
    Aug 13, 2010 at 18:54
  • Not exactly what your looking for, but there is a sweet open source data picker at toedter.com/en/jcalendar/index.html
    – Andy
    Aug 31, 2010 at 22:20

1 Answer 1

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.