Question

I have created a JSpinner and linked a SpinnerDateModel to it like this.

 travelTimeModel = new SpinnerDateModel();
 travelTimeModel.setCalendarField(Calendar.MINUTE);

 spinnerTravelTime = new JSpinner();
 spinnerTravelTime.setModel(travelTimeModel);
 spinnerTravelTime.setEditor(new JSpinner.DateEditor(spinnerTravelTime, "HH:mm"));

When I load the code the JSpinner shows the current time, so this part works.

However I'd like to change the value of the JSpinner according to a Calendar object received...

How can I change/add the time from the calendar object to the JSpinner and how should I read the time from the JSpinner into a Calendar object?

Was it helpful?

Solution

Spinner Date model doesn't support Calender interaction directly. However, you can create Date from Calender instance and work with it:

public SpinnerDateModel(Date value,  Comparable start, Comparable end, int calendarField)

And then if you need to change the value, you will have to use:

public void setCalendarField(int calendarField)

function, which Changes the size of the date value change computed by the nextValue and previousValue methods. The calender fields are one of:

Calendar.ERA
Calendar.YEAR
Calendar.MONTH
Calendar.WEEK_OF_YEAR
Calendar.WEEK_OF_MONTH
Calendar.DAY_OF_MONTH
Calendar.DAY_OF_YEAR
Calendar.DAY_OF_WEEK
Calendar.DAY_OF_WEEK_IN_MONTH
Calendar.AM_PM
Calendar.HOUR
Calendar.HOUR_OF_DAY
Calendar.MINUTE
Calendar.SECOND
Calendar.MILLISECOND 

Check out the SpinnerDateModel documentation, for more details.

OTHER TIPS

I used the feedback to make the following solution:

    DateFormat dateFormat = new SimpleDateFormat("HH:mm");
    try {
        Date date = dateFormat.parse("00:00");
        travelTimeModel.setValue(date);
    } catch (ParseException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top