JTextField.focusLost is called multiple times. Why?

I have a JTextField component called projectNum.

On focusLost I'm trying to prevent user to leave an empty field.

But I have a problem, because focusLost is called two times.

Here is a code snippet:

projectNum.addFocusListener(new FocusAdapter(){

publicvoid focusGained(FocusEvent e){}

publicvoid focusLost(java.awt.event.FocusEvent e){

if(!e.isTemporary()){//only for permanent lost of focus

if (validProjectNum()){

newProject.setProjectNum(Integer.valueOf(projectNum.getText().trim()).intValue());

}else{

if(!projectNum.hasFocus()){

projectNum.requestFocus();

}

}

}

}

}

});

Method "validProjectNum()" simply checks if JTextField is empty and

if so, shows JOptionPane message and returns false.

Problem is that I'm geting that message twice

(four times if I exclude: if(!e.isTemporary()) ).

I can't figure out who is calling focusLost the second time.

How can I prevent it to be called only once.

I appreciate any help or hint.

Thanks.

[1830 byte] By [Toxtera] at [2007-10-2 10:22:44]
# 1
Use an InputVerifier instead of handling the focusLost() event.
camickra at 2007-7-13 1:55:03 > top of Java-index,Desktop,Core GUI APIs...
# 2

> Use an InputVerifier instead of handling the

> focusLost() event.

Well, I've tried that allready but It doesn't work.

JOptionPane.showMessageDialog is never called.

Here is a code snippet from it:class MyVerifier extends InputVerifier {

public boolean verify(JComponent input){

return validProjectNum();

}

}

projectNum.setInputVerifier(new MyVerifier());

Toxtera at 2007-7-13 1:55:03 > top of Java-index,Desktop,Core GUI APIs...
# 3

class MyVerifier extends InputVerifier

{

public boolean verify(JComponent input)

{

JTextField tf = (JTextField)input;

if(tf.getText().equals("") return false;

return true;

}

}

Michael_Dunna at 2007-7-13 1:55:03 > top of Java-index,Desktop,Core GUI APIs...
# 4

> > class MyVerifier extends InputVerifier

> {

>public boolean verify(JComponent input)

>{

>JTextField tf = (JTextField)input;

>if(tf.getText().equals("") return false;

>return true;

>}

> }

>

OK, but where to show a message to a user?

This only prevents user from leaving a field if is empty.

Thanks.

Toxtera at 2007-7-13 1:55:03 > top of Java-index,Desktop,Core GUI APIs...
# 5
> OK, but where to show a message to a user?if the textfield is empty, this line executesif(tf.getText().equals("")) return false;perhaps you could add your message as part of the 'block' of this if statement
Michael_Dunna at 2007-7-13 1:55:03 > top of Java-index,Desktop,Core GUI APIs...
# 6

> > OK, but where to show a message to a user?

>

> if the textfield is empty, this line executes

> if(tf.getText().equals("")) return false;

>

> perhaps you could add your message as part of the

> 'block' of this if statement

Well, I've tried that allready (see my second post, method validProjectNum()

does exactly that).

Anyway, I solved the problem using focusLost and a little trick:jTextField.requestFocus();

jTextField.setFocusable(false);

JOptionPane.showMessageDialog(null, "error message");

jTextField.setFocusable(true);

jTextField.requestFocus();

So as you can see, key thing is to temporary disable focus on component

and than enable it back after JOptionPane.showMessageDialog().

It looks like JOptionPane.showMessageDialog()

triggers focusLost() event.

Thanks everyone.

Toxtera at 2007-7-13 1:55:03 > top of Java-index,Desktop,Core GUI APIs...
# 7

also works like this

class MyVerifier extends InputVerifier

{

public boolean verify(JComponent input)

{

JTextField tf = (JTextField)input;

if(tf.getText().equals(""))

{

input.setInputVerifier(null);

JOptionPane.showMessageDialog(null,"Error, textfield empty");

input.setInputVerifier(this);

return false;

}

return true;

}

}

Michael_Dunna at 2007-7-13 1:55:03 > top of Java-index,Desktop,Core GUI APIs...
# 8

> also works like this

>

> ...

>input.setInputVerifier(null);

>JOptionPane.showMessageDialog(null,"Error, "Error, textfield empty");

>input.setInputVerifier(this);

...

>

That did the trick!

I like this more than manipulating with focusLost and focusGained.

This is much more elegant solution, because focus is never lost from this

component (unlike focusLost which reacts after component lost its focus).

Thank you very much!

Toxtera at 2007-7-13 1:55:03 > top of Java-index,Desktop,Core GUI APIs...