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.
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 ?
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);
}