Solved

focusLost event

Posted on 2004-11-02
294 Views
Last Modified: 2012-06-27
Hi,
  I have a JTextField and when focusLost event is triggered it get details from the database and fill the other fields.
So there is a check there to make sure the JTextField is not empty to do so.  That is grand but if focus is on this JTextField and I hit the canel button I get the message the JTextField cannot be empty.

Is there anyway to find out in the FocusLost if the next component to get the focus is a Cancel button?

Or is there another way to do this.

thanks
0
Question by:Bourke
    3 Comments
     
    LVL 2

    Expert Comment

    by:tdisessa
    Should the Cancel button be checking if any of the fields are empty if you are just cancelling the form?
    0
     
    LVL 14

    Accepted Solution

    by:
    Hope this model might help you

    import javax.swing.*;
    import java.awt.event.*;
    /*
     * MyTextFieldText.java
     *
     * Created on November 2, 2004, 10:49 PM
     */

    /**
     *
     * @author  Sudhakar
     */
    public class MyTextFieldText extends javax.swing.JFrame {
       
        JTextField field=new JTextField("field");
        JTextField field1=new JTextField("field1");
        JTextField field2=new JTextField("field2");
        JButton b=new JButton("Cancel");
        public MyTextFieldText() {
           
           MyFocusListener listener= new MyFocusListener();
            field1.addFocusListener(listener);
            field2.addFocusListener(listener);
            b.addFocusListener(listener);
        }
       
        public static void main(String args[]) {
            new MyTextFieldText().show();
        }
       
      class MyFocusListener extends FocusAdapter {
            public void focusGained(FocusEvent evt) {
            if(evt.getSource()!=b)
            {
               if(field1.getText().trim().length()==0)
               {
                   field1.requestFocus();
                   return;
               }
            }                
            }        
        }
      }
    0
     

    Author Comment

    by:Bourke
    sudhakar

    The code you gave me above is what I have, but it made me re-look at my own code and found I should not have had a Dialog where it should not be.

    Thanks for your help
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    java ^ examples 8 40
    bigHeights  challenge 13 28
    count8 challlenge 13 34
    How Complex Is This Java Course ? 9 32
    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now