GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes JSpinner.DateEditor - Set default focus on dd Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JSpinner.DateEditor - Set default focus on dd" Watch "JSpinner.DateEditor - Set default focus on dd" New topic
Author

JSpinner.DateEditor - Set default focus on dd

Lisa Zapson
Ranch Hand

Joined: Nov 22, 2004
Posts: 50
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

Joined: Jul 26, 2007
Posts: 10425
    
    8

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.



[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Lisa Zapson
Ranch Hand

Joined: Nov 22, 2004
Posts: 50
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

Joined: Sep 20, 2005
Posts: 862
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.



bitguru blog
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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

Joined: Jun 09, 2003
Posts: 4632
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSpinner.DateEditor - Set default focus on dd