• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSpinner.DateEditor - Set default focus on dd

 
Lisa Zapson
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using a JSpinner with a SpinnerDateModel and a DateEditor. The DateEditor is setup with the following format.

new JSpinner.DateEditor(startDateS, "yyyy-MM-dd");

When I click on the spinner the year gets incremented. I would like to set the day (dd) to get incremented. Can you give me any suggestions to set the day to be incremented, without double clicking on the day before clicking on the spinner?

Thanks for all your help, Lisa
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lisa Zapson:
I Can you give me any suggestions to set the day to be incremented, without double clicking on the day before clicking on the spinner?


You need to override the getNextValue in the spinner date model.

 
Lisa Zapson
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overriding the getNextValue in the spinner date model works but it always increments the day, even if the focus is on the year. I would like the spinner to increment the day when the focus is on the day.

I would like to set the initial focus to the day (dd) instead of the year.

Thanks for your help, I really appreciate it. Lisa
 
Brian Cole
Author
Ranch Hand
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lisa Zapson:
Overriding the getNextValue in the spinner date model works but it always increments the day, even if the focus is on the year. I would like the spinner to increment the day when the focus is on the day.

I would like to set the initial focus to the day (dd) instead of the year.


Because of private methods and such, this is actually nontrivial to achieve.

The easiest way I could come up with is to set a custom Caret. Yes, this is an ugly hack, but it seems to work.

 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't you just set the calendar field of the SpinnerDateModel? As far as I just read the code, that seems to be what the default JSpinner and its UI are using too, and what SpinnerDateModel is using to determine the next value.
 
Brian Cole
Author
Ranch Hand
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
Can't you just set the calendar field of the SpinnerDateModel? As far as I just read the code, that seems to be what the default JSpinner and its UI are using too, and what SpinnerDateModel is using to determine the next value.


I didn't explore that approach fully, but IIRC the underlying JFormattedTextField's formatter was resetting things upon focus gain.

I'd be curious to see if you can get it working.
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, but that can be easily remedied - at the cost of being able to change other fields.

If you create a subclass of SpinnerDateModel that sets the calendar field to Calendar.DAY_OF_YEAR in the constructor using the call to super.setCalendarField, and overrides setCalendarField to do nothing, then you will be able to change the day field - but ONLY the day field.


I've also tried storing the field upon focus lost and resetting it upon focus gained, but the UI (not the formatter) yet again sets the calendar field.

I've located the cause of the problem. BasicSpinnerUI has a static class for handling the button clicks. This button calls a private method called "getCalendarField". This method indeed uses the formatter and the selection to determine which calendar field to use.

The precise code is the following (start is the text selection start):

So now you know where to look. Either try to change the selection start, or change the formatter so it will return the date field if the selection start is 0. However, that gives the problem that if you select the entire year, you'll change the day to.

Difficult problem indeed.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Brian Cole
Author
Ranch Hand
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Godbole's code and Mr. Dunn's code both prevent
the user from incrementing/decrementing the year or
month with the arrow buttons or up/down keyboard keys.

With my Caret hack I was trying to preserve that
functionality without having to reimplement too
much unoverridable code from the JDK.

[edit: Is unoverridable a word?]
[ January 16, 2008: Message edited by: Brian Cole ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic