JTextField.requestFocus problem

M

Matthew Bailey

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();
}
}
}
});
 
J

John C. Bollinger

Matthew said:
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
(e-mail address removed)
 
G

Giulio Alfieri

Hi,

I'm new to Java and it's a Hobby for me... but, why you don't use the
inputVerifier to check the value on JtextFiel instead construct a method
under focus lost?

regards

Giulio
Matthew said:
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
(e-mail address removed)
 
M

Matthew Bailey

This code should not allow you to exit a field w/ invalid content, this
stops the person from ever having 2 text fields w/ invalid content.
Matthew said:
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
(e-mail address removed)
 
M

Matthew Bailey

It seems like there was a reason... although it escapes me now... maybe
I will try that again to remember why!

Giulio said:
Hi,

I'm new to Java and it's a Hobby for me... but, why you don't use the
inputVerifier to check the value on JtextFiel instead construct a method
under focus lost?

regards

Giulio
Matthew said:
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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top