JTextField.requestFocus problem

Discussion in 'Java' started by Matthew Bailey, Jul 6, 2004.

  1. 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();
    }
    }
    }
    });
     
    Matthew Bailey, Jul 6, 2004
    #1
    1. Advertising

  2. 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
     
    John C. Bollinger, Jul 6, 2004
    #2
    1. Advertising

  3. 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

    John C. Bollinger wrote:
    > 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
    >
     
    Giulio Alfieri, Jul 7, 2004
    #3
  4. 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.

    John C. Bollinger wrote:

    > 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
    >
     
    Matthew Bailey, Jul 8, 2004
    #4
  5. It seems like there was a reason... although it escapes me now... maybe
    I will try that again to remember why!

    Giulio Alfieri wrote:

    > 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
    >
    > John C. Bollinger wrote:
    >
    >> 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
    >>

    >
    >
     
    Matthew Bailey, Jul 8, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotr Wypych

    J2ME: TextField.requestFocus() ??

    Piotr Wypych, Nov 28, 2003, in forum: Java
    Replies:
    1
    Views:
    2,721
    Darryl L. Pierce
    Nov 30, 2003
  2. Katie Moore

    JTextField Problem

    Katie Moore, Dec 1, 2003, in forum: Java
    Replies:
    1
    Views:
    490
    Harald Hein
    Dec 1, 2003
  3. blue
    Replies:
    1
    Views:
    1,196
    Babu Kalakrishnan
    Dec 11, 2004
  4. JariTapio
    Replies:
    2
    Views:
    2,207
    JariTapio
    Mar 31, 2005
  5. Badshah

    JTextField Unicode Problem

    Badshah, Jan 29, 2007, in forum: Java
    Replies:
    0
    Views:
    381
    Badshah
    Jan 29, 2007
Loading...

Share This Page