Listener question

Hi,

I have two text fields txt1 and txt2. I have an add button too.

I disable the add button at first and enable it only if the text contents

of the two text fields is not empty.

I thought the focus listener would be ideal to use . But, if the order the

way the user enters the text in the two text fields is not considered(i.e he may

enter value for txt1 and then for txt2 or vice versa), then the focus listener may

not be the right event listener. Please could someone guide me. Thanks in advance.

[555 byte] By [seemap123a] at [2007-10-3 5:14:44]
# 1
add the focus listener to both fields... then inside the focusLost method test BOTH fields to make sure there is text in them. That way no matter which field goes first they must BOTH have text before the button get Enabled
JohnKelley1983a at 2007-7-14 23:21:17 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks for the quick response. I tried it. But with focus listener the user always has to click somewhere else for the code to execute. Is there any other listener that would accomplish other than the focus listener?
seemap123a at 2007-7-14 23:21:17 > top of Java-index,Desktop,Core GUI APIs...
# 3
Use a DocumentListener on the text fields.
camickra at 2007-7-14 23:21:17 > top of Java-index,Desktop,Core GUI APIs...
# 4

Thanks for the reply. But, I'm still not sure how I would check if both the text field's text is empty using a document listener.

With the focus listener I had done it as :

class TxtFocusListener implements FocusListener{

public void focusGained(FocusEvent e) {

}

public void focusLost(FocusEvent e) {

if((getTxtField().getText()!= null && getTxtField().getText().trim().length() > 0)

&& (getTxtValue().getText()!= null && getTxtValue().getText().trim().length()>0)){

getAddButton().setEnabled(true);

}

else{

getAddButton().setEnabled(false);

}

}

}

Please any guidance ?

seemap123a at 2007-7-14 23:21:17 > top of Java-index,Desktop,Core GUI APIs...
# 5

JTextFields tf1 and tf2, JButton btn

tf1.getDocument().addDocumentListener(new DocumentListener(){

public void changedUpdate(DocumentEvent e){checkTF();}

public void removeUpdate(DocumentEvent e){checkTF();}

public void insertUpdate(DocumentEvent e){checkTF();}

});

tf2.getDocument().addDocumentListener(new DocumentListener(){

public void changedUpdate(DocumentEvent e){checkTF();}

public void removeUpdate(DocumentEvent e){checkTF();}

public void insertUpdate(DocumentEvent e){checkTF();}

});

}

public void checkTF()

{

btn.setEnabled(tf1.getDocument().getLength() > 0 &&

tf2.getDocument().getLength() > 0);

}

Michael_Dunna at 2007-7-14 23:21:17 > top of Java-index,Desktop,Core GUI APIs...
# 6
Michael_Dunn ,Thanks a lot. It worked as desired !
seemap123a at 2007-7-14 23:21:17 > top of Java-index,Desktop,Core GUI APIs...