0

OK so there are two parts to this question the first is how can I return JUST the minute second millisecond (mm:ss.SSS) from a JSpinner. I have a JSpinner set up now that's using java's DateEditor format in a JSpinner like this:

JSpinner spinner = new JSpinner();
spinner.setModel(new javax.swing.SpinnerDateModel());
spinner.setEditor(new javax.swing.JSpinner.DateEditor(spinner, "mm:ss.S"));
spinner.setBounds(420, 140, 228, 20);
contentPane.add(spinner);

But the problem is that it will return a string such as "Wed Jun 06 00:26:12 MDT 2012" when I only want to return the "minute:second.millisecond" and not the day, month or any other information.

The second part of the question is how can I only display and change the the millisecond to the tenth place, so instead of displaying as (mm:ss.SSS) it displays as (mm:ss.S) and the S value can be changed to a value between 0 and 9 instead of 0 and 999 the value also need to return this way.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
clankfan1
  • 225
  • 2
  • 3
  • 10

3 Answers3

1

You could use a Calendar to extract minute, second and millisecond. Here is an example:

SpinnerDateModel model = (SpinnerDateModel) spinner.getModel();
Calendar time = Calendar.getInstance();
time.setTime(model.getDate());
// Access minute, second, millisecond
time.get(Calendar.MINUTE);
time.get(Calendar.SECOND);
time.get(Calendar.MILLISECOND);

Setting default value should work as follows:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);

SpinnerDateModel model = new SpinnerDateModel(cal.getTime(), null,
            null, Calendar.SECOND);

JSpinner spinner = new JSpinner();
spinner.setModel(model);
sebastian
  • 2,427
  • 4
  • 32
  • 37
1

Concerning the first part of the question... I got a similar problem awhile ago, I needed a spinner that managed Hours/Minutes/Seconds.

I wrote an new implementation of the JSpinner class, you can use it as a basis and update it to get a Minutes/Seconds/Milliseconds spinner. I share with you 'as is', it fits my personnal needs, but I can certainly be improved :

/**
 * TimeSpinner
 * A implementation of JSpinner that manages
 * only the hour/minute part of a date.
 * Careful, in consequence the getValue()
 * gives you a date based on the 01/01/1970
 * ('issue' known about JSpinner).
 * => This spinner implements its own model. It allows to fix
 * a pb about the tick (the regular model SpinnerDateModel
 * increases/decreases hours instead of minutes.
 * => It overrides setBackground and make it works (the regular
 * JSpinner.setBackground does not).
 *
 * User: Yannick DECOURTRAY
 * Date: 21/06/11
 */
public class TimeSpinner extends JSpinner {

    /**
     * Constructor
     */
    public TimeSpinner() {
        Date date = today();
        TimeSpinnerModel sm = new TimeSpinnerModel(date, null, null, Calendar.MINUTE);
        setModel(sm);
        JSpinner.DateEditor de = new JSpinner.DateEditor(this, "h:mm a");
        setEditor(de);
    }

    /**
     * Calls setBackground on Editor of the spinner
     * @see javax.swing.JComponent#setBackground(java.awt.Color)
     */
    @Override
    public void setBackground(Color bg) {
        JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) getEditor();
        editor.getTextField().setBackground(bg);
    }

    /**
     * Model class
     */
    private class TimeSpinnerModel extends SpinnerDateModel {

        /**
         * Constructor
         * @param value Current date
         * @param start Low limite date
         * @param end High limit date
         * @param calendarField Step of incrementation
         */
        public TimeSpinnerModel(Date value,
                                Comparable start,
                                Comparable end,
                                int calendarField) {
            super(value, start, end, calendarField);
        }

        /** @see javax.swing.SpinnerDateModel#getNextValue() */
        public Object getNextValue() {
            Date currentDate = getDate();
            Calendar currentCal = Calendar.getInstance();
            currentCal.setTime(currentDate);
            int calField = Calendar.MINUTE;//getCalendarField();

            // Add calField to currentDate
            currentCal.add(calField, 1);
            Date newDate = new Date(currentCal.getTimeInMillis());

            Date endDate = (Date) getEnd();
            if (endDate != null && newDate.after(endDate))
                return currentDate;
            else
                return newDate;
        }

         /** @see javax.swing.SpinnerDateModel#getPreviousValue() */
        public Object getPreviousValue() {
            Date currentDate = getDate();
            Calendar currentCal = Calendar.getInstance();
            currentCal.setTime(currentDate);
            int calField = Calendar.MINUTE;//getCalendarField();

            // Add calField to currentDate
            currentCal.add(calField, -1);
            Date newDate = new Date(currentCal.getTimeInMillis());

            Date startDate = (Date) getStart();
            if (startDate != null && newDate.before(startDate))
                return currentDate;
            else
                return newDate;
        }
    }

    /**
     * Gets the today date
     *
     * @return the today date
     */
    private static Date today() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
}
Yanflea
  • 3,876
  • 1
  • 14
  • 14
0

Instead of using the DateEditor, you should use a custom JSpinner.Editor.

Here's an example, very similar to what you want, of doing this : http://www.java2s.com/Code/Java/Swing-JFC/AnexampleofJSpinnerwithacustomeditor.htm

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758