Matthew Bailey wrote:
> I have an applet which contains text fields that need validation on
> event focusLost. Everything works perfect until I try to to the
> JTextField.requestFocus at which time the Dialog box displays 3 or 4
> times and then no longer allows you to edit any text fields.
>
> I don't understand why this is hapenning.
>
> Here is the code I'm using to handle the focusLost event:
>
> public void focusLost(java.awt.event.FocusEvent e) {
> if (e.isTemporary()) {
> return;
> }
> javax.swing.JTextField textField =
> (javax.swing.JTextField)e.getSource();
> String content = textField.getText();
>
>
>
> if (content.length() != 0) {
> try {
> Integer.parseInt(content);
> } catch (NumberFormatException nfe) {
> getToolkit().beep();
>
> javax.swing.JOptionPane.showMessageDialog(null,
> "Invalid Character Entered!\nPlease
> enter whole positive numbers only!\nNo alpha characters allowed,
> including '#$.([{)]}' ",
> "Integer Field Error",
> javax.swing.JOptionPane.ERROR_MESSAGE);
> textField.requestFocus();
> }
> }
> }
> });
I don't know whether this particular problem is causing your issue, but
I observe that if the text field with focus contains invalid text, and
you then try to change the focus to another field with invalid text then
you will initiate an infinite loop in which each text field keeps
requesting the focus from the other. This would require that at least
one field be assigned invalid content before the focus listener is
installed on it -- perhaps a blank (but nonempty) string?
John Bollinger