Why does focusGained get recalled?

I am developing some dialogs which pop up as soon as a textfield subclass gets focus. This works fine, except that the focusGained method gets recalled, regardless of what I do - I have had to use a nasty hack to ignore the second invocation. Here is code , with the hack :

public void focusGained(FocusEvent e) {

if (eventcount++ % 2 != 0) {

KeyboardFocusManager manager =

KeyboardFocusManager.getCurrentKeyboardFocusManager();

manager.focusNextComponent();

return;

}

DataTypeDialog dialog = new DataTypeDialog(null, "Data Type");

dialog.setVisible(true);

DataTypeField.this.setText(dialog.getValue());

dialog.dispose();

}

Below is the code I was hoping would work, but doesn't :

public void focusGained(FocusEvent e) {

DataTypeDialog dialog = new DataTypeDialog(null, "Data Type");

dialog.setVisible(true);

DataTypeField.this.setText(dialog.getValue());

dialog.dispose();

KeyboardFocusManager manager =

KeyboardFocusManager.getCurrentKeyboardFocusManager();

manager.focusNextComponent();

}

Any ideas why this hack is required - or indeed if I am going about the problem in the wrong way?

I look forward to any comments.

[1274 byte] By [dalemana] at [2007-11-27 4:52:28]
# 1

It gains focus when

a) you tab or click on the field

b) the dialog closes.

Is that what you mean by the focusGained method gets recalled?

So somehow you need to track whether focus was as a result of closing the dialog or not. If focus was gained because you closed the dialog then you don't show the dialog again.

camickra at 2007-7-12 10:06:29 > top of Java-index,Desktop,Core GUI APIs...
# 2
Aha - yep that makes sense - thanks
dalemana at 2007-7-12 10:06:29 > top of Java-index,Desktop,Core GUI APIs...