This discussion is archived
2 Replies Latest reply: Jan 23, 2013 11:40 PM by 986686 RSS

How to set the text selected in JAVA JSpinField on focus event ?

986686 Newbie
Currently Being Moderated
Hi,
I am new to JAVA swing.

Can any one tell me , how to set the whole text selected in JAVA jspinfield when on focus on this form field (when tab pressed while cursor was in previous form field ).

The aim is to replace the existing time/text when user type the value manually after focus came came to this field.

I know for normal textfiled , we can set field.selectAll() on onfocus event.

But for jspinfield , there is no method like selectAll().

And the textfield (JTextField) used in jspinfield is a protected variable.

Can any one help me out to set the text selected when onfocusing the jspinfield .

Below is the code that i am trying to build for this.

timeSpentSpin = new JSpinField();
timeSpentSpin.setFocusable(true);
timeSpentSpin.addFocusListener(new java.awt.event.FocusAdapter() {
     
     public void focusGained(java.awt.event.FocusEvent evt) {                 
          SwingUtilities.invokeLater( new Runnable() {                       
                    @Override
                    public void run() {
               // kind of below one. But there is no method like that
                    // timeSpentSpin.selectAll();

                              
                    }
               });
     }
     });

Thanks in Advance,
Madeswaran.
  • 1. Re: How to set the text selected in JAVA JSpinField on focus event ?
    Jörg Explorer
    Currently Being Moderated
    I am new to JAVA swing
    Welcome. Please read the sticky post on top of the forum (How to post code). And here is another link which when followed increases the chance of receiving proper help.
    how to set the whole text selected in JAVA jspinfield
    ... new JSpinField();
    I don't know a JSpinField class in java. I do, however, know the JSpinner class and can tell you how to select its text.
    If JSpinField is your extension to JSpinner, then I wonder why you want to have it all selected. The purpose of a date spinner is to encrease or decrease parts of a given date (dd.MM.yyyyy). If you want to enter a complete date, I would recommend to use a JFormattedTextField with a DateFormatter instead.
    But if you insist on using the spinner, let me know.
  • 2. Re: How to set the text selected in JAVA JSpinField on focus event ?
    986686 Newbie
    Currently Being Moderated
    Hello,
    Thanks for your reply. yes JSpinField is the extension to JSpinner . It was part of jcalendar-1.4.jar library that is available for public in the url http://www.toedter.com/en/jcalendar/

    Now , i modified the source of the jcalendar-1.4.jar library by editing the JSpinField.java class.
    Here i updated the JTextField used along with JSpinner variable to public access, previously it was protected.

    Now i could be able to apply selectAll() method to that text field and my problem got solved.

    Thanks.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points