0

Not much to add to the Title question.

Here's what tabbing to the first box looks like, with cursor before the first character in the field, so that the user will have to delete the character if he wishes to enter his own month, day, or year number:

enter image description here

Here's what I'd like, when the field is tabbed to (or otherwise selected), so the user doesn't have to delete the character(s) presented if he wishes to enter his own year, etc.:

enter image description here

I can accomplish this for a JTextField like so, for example:

txtDateFrom.select(0,99);

But .select() isn't a method for JSpinner.

(I realize that this raises the question, "Why use a spinner?" but the obvious answer is that I'd like both methods of selecting to be available, as is common in design.)

(A much less pressing but related matter... I made an integer array of 100 year-dates [e.g., 2014] named years and used SpinnerListModel(years) because when using the SpinnerNumberModel, a year would be displayed as 2,014. I can live with what I've done, but is there a less-brute-force way? There's no method containing "format" that I could find for this method.)

DSlomer64
  • 4,234
  • 4
  • 53
  • 88
  • If I use a `JTextField` that has been initialized to, for example, `01/18/2014`, Java has made it very user-friendly in that double-clicking the "mm" or "dd" or "yyyy" portion of the mm/dd/yyyy field highlights that portion so that user entry overwrites the selection without having to delete--e.g., 01/`18`/2014. I may revert to that if there's no relatively easy fix to my problem as posted above. – DSlomer64 Jan 20 '14 at 02:40
  • 2
    I think that your problem is well described at this possible duplicate [Make JSpinner Select Text When Focused](http://stackoverflow.com/questions/15328185/make-jspinner-select-text-when-focused). Also one of the solutions will likely work for you. – Hovercraft Full Of Eels Jan 20 '14 at 03:13
  • @HovercraftFullOfEels--You're right about this being a duplicate. The suggestions at the link are dicey and hard to follow, so I'll have to study them more carefully. Seems a lot to go through for such a common need. I may settle for VGR's 2-line side note, as it makes my 3-spinner method look childish and is simple to implement as is. – DSlomer64 Jan 20 '14 at 19:42
  • DSlomer: VGR's recommendations are no different from Rob Camick's recommendations (camickr) in the link I provide, and Rob's answer provides more information than VGR's. I could have posted the same thing yesterday, but figured why repeat what's already out there? The problem with both recommendations is that while they seem to work for Window's platform, they don't work for most Macs which is why MadProgrammer added the extra delay. A Swing Timer could also provide a similar delay if need be. – Hovercraft Full Of Eels Jan 20 '14 at 21:34
  • My apologies if I posted something that was already in that link. I read through it and I saw suggestions to use a single invokeLater, and I saw suggestions to use a heuristic delay, but I didn't actually see any suggestions that made use of two invokeLater calls (one inside the other). I would be interested in learning whether the double-invokeLater approach works in OS X. – VGR Jan 21 '14 at 14:33
  • @Hover--I only "liked" VGR's 'two-line side note' because it was easily implemented to do exactly what I needed at the moment for Windows--i.e., short-sighted, unprofessional--but I'm just messin' around, learnin', half-serious. I did up-vote you a day or two ago, and always appreciate your clarity in answering. It's just that I don't have the background to appreciate Rob's recs. Like I said, I need to (eventually) go back and study them. But you've just made "eventually" seem "sooner". Anyway, thanks. – DSlomer64 Jan 22 '14 at 19:12

1 Answers1

2

This works in Java 1.7.0_51, in Windows and Linux. I don't have the ability to test it in OS X.

JSpinner.DefaultEditor editor =
    (JSpinner.DefaultEditor) spinner.getEditor();

editor.getTextField().addFocusListener(new FocusAdapter() {
    @Override
    public void focusGained(FocusEvent event) {
        final JTextField textField = (JTextField) event.getComponent();
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        textField.selectAll();
                    }
                });
            }
        });
    }
});

Side note: Have you considered replacing your three JSpinners with a single JSpinner like this?

JSpinner spinner = new JSpinner(new SpinnerDateModel());
spinner.setEditor(new JSpinner.DateEditor(spinner, "MM/dd/yyyy"));

The up/down arrow buttons (and arrow keys) will change whichever field contains the text cursor.

It won't solve your focus issue, but you may decide that the issue is less of an issue.

VGR
  • 40,506
  • 4
  • 48
  • 63
  • @VGR--I am new to editors, so I'll have to do some searching and reading, but I plan to try your 2-line "side note". Thanks. – DSlomer64 Jan 20 '14 at 19:35
  • That didn't take long--I like it better than my 3 spinners by FAR! So double thanks. I still need to read about .setEditor, etc. I found it last night in a text, but the example wasn't two lines, so I put it off. Your two lines are a godsend. – DSlomer64 Jan 20 '14 at 19:46
  • P.S. I was disappointed to get null pointer exception, but relieved that it was so easily fixed: `spinner.setValue(new Date());`... That put today's date in as default spinner value: 01/20/2014#, where '#' is the spinner up-down objects. – DSlomer64 Jan 20 '14 at 19:55
  • Imagine my surprise when it took me too many minutes to realize that, in the "MM/dd/yyyy" format string, cASe mATteRS! In short, "mm" represents minutes. Other than that dopey move I made, I'm lovin' your "side note", as it has become MY MAIN note! So clean! MANY MORE THANKS! – DSlomer64 Jan 20 '14 at 21:50