2

I'm trying to set a JSpinner to run from hour 00:00 to 02:00.

So I built this code:

import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;


public class HourSpinner2 extends javax.swing.JFrame {


    javax.swing.JSpinner jSpinner2;

    public HourSpinner2() {

        jSpinner2 = new javax.swing.JSpinner();
        add(jSpinner2);

// Option 1 : set range

jSpinner2.setModel(new SpinnerDateModel(new java.util.Date(1388498400000L), new java.util.Date(1388484000000L), new java.util.Date(1388505600000L), java.util.Calendar.MINUTE));

//  Option 2 : set HH:mm format 

jSpinner2.setModel(new SpinnerDateModel(new java.util.Date(1388498400000L), null, null, java.util.Calendar.MINUTE));
            JSpinner.DateEditor de = new JSpinner.DateEditor(jSpinner2, "HH:mm");
            de.getTextField().setEditable( false );
            jSpinner2.setEditor(de);

    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new HourSpinner2().setVisible(true);
            }
        });
    }

 }

What I find out is that I can do only one of the 2 at a time. Either set a range or set a format. Doing both would simply freeze the component for user edit.

How to make this work for both range and format?

yaniv
  • 152
  • 5
  • 15
  • 1) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 2) Please don't forget to add a '?' to questions! Some people do a search in the page for '?' and if none exists in the 'question' go directly to the next (actual) question in line. I added a question to the ..question. Please review it carefully and change if necessary. – Andrew Thompson Dec 31 '13 at 15:36

1 Answers1

2

The issue with your code is that actually the formatter throws away the date fields and keeps only the time part of the current value. But then the value no longer lies between min and max and this is not changeable.

As a simple workaround you may specify the min and max values as in the following code:

Calendar cal1 = GregorianCalendar.getInstance();
cal1.clear();
cal1.set(1970, Calendar.JANUARY, 1, 0, 0);
Date min = cal1.getTime();

Calendar cal2 = GregorianCalendar.getInstance();
cal2.clear();
cal2.set(1970, Calendar.JANUARY, 1, 2, 0);
Date max = cal2.getTime();
Howard
  • 38,639
  • 9
  • 64
  • 83
  • Your answer is correct (I'm pretty sure), but the Dates should be derived using Calendar, not deprecated Date constructors. For instance, `Calendar cal = Calendar.getInstance(); cal.set(1970, Calendar.JANUARY, 1, 2, 0); Date date = cal.getTime();` – VGR Dec 31 '13 at 16:15
  • @VGR Of course you're right. Changed the code to reflect your remarks (but do not forget the call to `clear`). – Howard Dec 31 '13 at 16:32
  • One more question please, after performing all the above, do you know how to set the jspinner focus to be on the minute and not the hours? (meaning, pressing the up button would increase the minutes rather than the hours) – yaniv Jan 01 '14 at 13:40
  • OK , about the focus issue i found the answer on http://stackoverflow.com/questions/16470613/howto-select-jspinner-date-field-when-jspinner-gets-focus – yaniv Jan 02 '14 at 08:35