Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

Catching TAB event

Thread view: 
Allan Valeriano - 05 Sep 2007 23:46 GMT
Hi all,

I have a JPanel with a JFormattedTextField inside of it. This field is
suposed to receive just dates on it, so I have this method to auto
complete the date when I type something.

private void setDateFieldListeners(final SimpleDateFormat dateFormat)
{
     addPropertyChangeListener(new PropertyChangeListener() {
       public void propertyChange(PropertyChangeEvent e) {
         if (e.getPropertyName() == "value") {
           try {
             Date date =
dateFormat.parse(FormattedDateField.this.getText());
             if (date != null && DateUtil.getYear(date) < 1000) {
               date = DateUtil.addYears(date, 2000);
               FormattedDateField.this.setValue(date);
             }
           }
           catch (ParseException p) {
           }

         }
       }
     });
     addKeyListener(new KeyListener() {
       public void keyPressed(KeyEvent ev) {
       }
       public void keyTyped(KeyEvent ev) {
       }
       public void keyReleased(KeyEvent ev) {
         int code = ev.getKeyCode();
         if (code == KeyEvent.VK_BACK_SPACE ||
             code == KeyEvent.VK_DELETE) {
           return;
         }
         String date = FormattedDateField.this.getText();
         String format = dateFormat.toPattern();
         int firstBarIndex = format.indexOf("/");
         int firstToSecondBar = -1;
         if (firstBarIndex != -1) {
           String auxString = format.substring(firstBarIndex + 1);
           firstToSecondBar = auxString.indexOf("/");
         }
         if ((date.matches("^[0-9]{" + firstBarIndex + "}") &&
                date.indexOf("/") == -1) ||
              (firstToSecondBar!=-1 &&
                date.matches("^[0-9]{1,}/[0-9]{" + firstToSecondBar +
"}"))) {
           FormattedDateField.this.setText(date + "/");
           return;
         }
       }
     });
   }

This JPanel is inserted on another panel, and I want the auto complete
to happen only when I leave the field.
I've tried adding a KeyListener to it, so I could filter the auto-
complete to happen only when VK_TAB is received, but it seems that
java doesn't catches TABs unless I set FocusTraversalKeysEnabled to
false (which I don't want to).
I also tried to catch it by overriding the method
getFocusTraversalKeys like that:

public Set getFocusTraversalKeys (int id) {
   if (id==KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS ||
       id==KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS) {
     //autocomplete
   } return super.getFocusTraversalKeys(id);
 }

This also seems not to work, cuz any key I press, this method is
called 3 times, receiving 0, 1 and 2 as id and
KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS is 0.

I have also tried adding a FocusListener to it, so I could capture the
event of loosing focus, but no success.

I tried also to do this:
http://www.exampledepot.com/egs/javax.swing.text/ta_OverrideTab.html?l=rel

And if I set the FocusTraversalKeysEnabled to false, it doesn't
traverse at all, and if I don't, the traverse event doesn't pass
through the actions.

Does anybody has another idea on catching the TAB?
Dennis Groenendijk - 06 Sep 2007 00:25 GMT
Hi Allan,

>Hi all,
>I have a JPanel with a JFormattedTextField inside of it. This field is
>suposed to receive just dates on it, so I have this method to auto
>complete the date when I type something.

[snipped code sample]

>This JPanel is inserted on another panel, and I want the auto complete
>to happen only when I leave the field.
[quoted text clipped - 21 lines]
>through the actions.
>Does anybody has another idea on catching the TAB?

I would not overwrite the getFocusTraversalKeys because this is basically an information method
and you want a specific action to be performed, unrelated to the information returned.
A FocusListener on the field should work if you implement the method on the focusLost method, so
I'm not sure why this did not work for you.
There is however an other way to accomplish the autocomplete, without changing the focus traversal keys, an
inputverifier

class AutoCompleteVerifier extends InputVerifier {
  public boolean verify(JComponent input) {
      // your autocomplete code
      // return true, if autocomplete succeeded or false if you want the user to enter something else
  }
}

You do setInputVerifier(autoCompleteVerifier) to your field and the verifier will be called when you leave the field.
The JComponent passed into the verify method is your field.

Hope this helps.

Regards,
Dennis
Allan Valeriano - 06 Sep 2007 14:35 GMT
> A FocusListener on the field should work if you implement the method on the focusLost method, so
> I'm not sure why this did not work for you.

Neither am I... :(

> There is however an other way to accomplish the autocomplete, without changing the focus traversal keys, an
> inputverifier
[quoted text clipped - 6 lines]
>
> }

That's not really what I need. My autocomplete always successes, but I
want it to happen *only* when I move the focus to another component...
Dennis Groenendijk - 06 Sep 2007 15:11 GMT
Hi Allan,

>> A FocusListener on the field should work if you implement the method on the focusLost method, so
>> I'm not sure why this did not work for you.
[quoted text clipped - 12 lines]
>That's not really what I need. My autocomplete always successes, but I
>want it to happen *only* when I move the focus to another component...

InputVerifier always fires (if you did not set setVerifyInputWhenFocusTarget(false) on the next field)  when the focus on tthe field is lost. So it seems to do what you need it to do and you can just return true in every circumstance. Otherwise post the problem you had with the focus listener, because that seems the solution that should work as wel.

Regards,
Dennis


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.