相关文章推荐
行走的冲锋衣  ·  spark ...·  2 月前    · 
追风的野马  ·  java - set percent ...·  1 年前    · 
不要命的西装  ·  Oracle之 UTL_FILE ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams // 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?

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, 2013 at 15:36

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();
                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, 2013 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, 2013 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 1, 2014 at 13:40
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.