Catching TAB event

  • Thread starter Allan Valeriano
  • Start date
A

Allan Valeriano

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?
 
D

Dennis Groenendijk

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.
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?

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
 
A

Allan Valeriano

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

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
}

}

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...
 
D

Dennis Groenendijk

Hi Allan,
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top